Templating tool
Steve Horne
sh at ttsoftware.co.uk
Thu Oct 26 10:31:58 EDT 2000
I have written a C++ program which translates a template file to
python and then calls the interpreter to run it. The templates can
look a bit like the following...
print """
This is some simple text -- Basic text
2 * 2 = <* 2 * 2 *> -- Calc result substituted
<** times 5
hello -- This line printed 5 times
**>
<** if a == b
hello -- This line printed only if a == b
**>
<** for i in [1, 2, 3]
line <* i *> -- This line printed three times
**>
<** for i in [1, 2, 3] if i != 2
line <* i *> -- This line printed two times
**>
"""
Special markers <*, *>, <** and **> are only interpreted within triple
quoted strings. Block structures (<** and **>) can be nested to any
depth.
As an example, you could write...
print """
<** for i in range (1, 3)
<** for j in range (1, 3)
<* i *> times <* j *> equals <* i * j *>
**>
**>
"""
and the output would be...
1 times 1 equals 1
1 times 2 equals 2
2 times 1 equals 2
2 times 2 equals 4
I assume this is similar to the way ASP works. I personally use it to
generate C++ code which is too complex for C++ templates or macros to
handle. For example, I have a template to generate scanner code, which
uses ternary search trees to specify the keywords.
So the question is, would anyone else find this useful?
--
Steve Horne
sh at ttsoftware.co.uk
More information about the Python-list
mailing list