I mentioned a templating language I put into Paste a while ago, but since then I extracted it into a separate package called Tempita. I think the documentation is fairly complete (it’s a small language), but I’ll describe it shortly here.
I wanted a text-substitution language, because I wanted something to be used to generate Python files, config files, etc. I also didn’t want a complex API, with search paths and components or something that interacts with import machinery, or any of that. string.Template is almost good enough, but not quite.
I started with the idea of something vaguely like Django Templates, though since I didn’t care about more advanced templating features like blocks that didn’t apply to my use cases. You do variable substitution with {{var|filter}}, and there’s no escape character, and that’s about where the similarity ends.
I realized there was no real reason to use anything but {{...}}, so it’s just {{if expr}}, {{endif}}, etc. There’s an escape for arbitrary Python, similar to how Kid does it — you can have blocks of Python code, but the Python code can only prepare variables and functions, it can’t write anything. I think this gives a nice escape for complex logic (for times when you can’t put the logic in a .py file), without the jumbled mish-mash of languages like PHP where you can trully mix functions and output.
Because it allows Python expressions everywhere, special tags don’t seem so necessary. Instead you can just provide functions to do whatever you need. I wrote a couple little ones as a start. There’s a few things that are awkward still, because there’s no way to define a block of template as a function, or pass the output of a block to a function. I haven’t actually needed these yet, but I can imagine needing this (e.g., when creating nested structures).
I wouldn’t suggest using this templating language in a web application, but I think it can be quite helpful for all the cases where you have to generate text and you aren’t writing a web application (e.g., a Framework Component). In my experience the web templating languages tend to be complex to invoke and understand in these contexts (and Buffet unfortunately doesn’t help in my mind, as it’s loading system is so vague).
No related posts.
I’d be interested to know what you thought of Twiddler:
http://www.simplistix.co.uk/software/python/twiddler
Using the plain text input would likely give you what you’re after in terms of simplicity, and then all your python goes in whatver python file you choose…
It looks like Twiddler is a template driven by external code? That’s definitely not what I’d be looking for — I want self-describing templates, that would usually be edited by a programmer anyway. Designers don’t edit templates for generating Python source code ;)
I’ve also found template languages of this style hard to follow, because key information is spread between two separate files.
Correct, I don’t believe in mixing template source with code that manipulates, ironically, for the same reason you give: I find it very confusing to have to try and determine what’s “template” and what’s “code”, especially when generating plain-text things like email where layout of the source is important.
I find the idea of the template being used to generate python source code too scary to contemplate ;-)
Generating code is a lot of what PasteScript does. But really it’s not that bad.
You just reinvented preppy:
http://www.reportlab.org/preppy.html
http://www.reportlab.org/downloads.html
There are some oddities about it, but essentially the language appears the same. The docs are likely out of date.
You are right, that does look extremely similar. Mine doesn’t compile to Python (I far prefer better error messages to the speed), and Python blocks are just slightly different syntactically. But otherwise, quite the same.
Not sure what you mean about “better error messages”. preppy (the current version) gives ordinary Python tracebacks with correct preppy source lines and preppy source line numbers.
It also has a feature where you can declare template signatures in the template {{def a, b, spam=’eggs’}} (I forget the precise syntax that was chosen, but very similar to that), and call templates like ordinary Python functions (I don’t think this feature is documented…). Otherwise the ordinary usage is import preppy; print preppy.getModule(‘mytemplate’).getOutput(dict(foo=’bar’)).
It was going to have a feature similar to PTL’s automatic escaping (using a different Python type for markup), but sadly I think that never got implemented (I think something went in in its place, but not sure exactly what).
One possible problem is the use of pycodegen and pyassem, which I think are strictly not public APIs of the stdlib. But I guess as long as ReportLab continues to maintain it (certainly preppy is used a lot inside ReportLab), that’s not a problem.