Comparing web.py's Templator and Jinja2: strengths and weaknesses
I'm adding a simple web interface to an already existing piece of software; web.py fits the job just right and that's what I'm using. Now I'm researching what templating engine to use and came down to two alternatives: either using web.py's own Templator or using Jinja2.
I already have both working in the app and I'm writing some very simple templates in both to explore them开发者_如何学JAVA. I must say I find Templator easier to read, that's probably due to me being a programmer and not a web designer (who would probably find Jinja easier?).
While I'm only generating (non compliant ;) ugly HTML pages now, I'll also use the templating engine to generate emails and good old plain text files.
Both softwares are "fast enough" for any practical purpose, I'd like to ask people who used extensively one or the other or both what are their strengths and weaknesses in the areas of ease of use, code cleanliness, flexibility, and so on.
Taking a quick look at Templator (which I have never used) and comparing it to Jinja2 (which I have used somewhat extensively), I'd say that the two are pretty similar ... but Templator is closer akin to Mako than to Jinja.
Mako and Jinja both support:
- Template Inheritance (You can have a layout that all your pages inherit from)
- Whitespace control
While Mako and Templator both support:
- Embedding "safe" Python in your templates.
All three support:
- Adding to the template context (functions, objects, variables, the works)
- Defining functions to encapsulate re-usable pieces of functionality in your templates (Jinja calls them "macros".)
- Conditionals and loops
- Setting and getting local variables.
- Expression evaluation
- Caching the compiled bytecode to speed up future execution.
Templator supports on weird thing that I don't believe either Jinja or Mako does:
- Setting attributes on the compiled template object from within the template code. (Speaking frankly, actually making use of that feature seems like the wrong thing to do. Any flags that your template could determine need to be set from what has been passed in by the context should already be set by your application code.)
Jinja takes the template code and compiles them to Python bytecode, but it does that for everything, rather than passing through strings to the Python interpreter to use safe_eval
. By doing so Jinja2 is theoretically immune to certain types of attacks on the template level (But when you have hostile input from your templates you generally have a much bigger problem).
As for the rest of it, it's all pretty much up to your preference for syntax.
What was hard for me in Templetor is template inheritance. Instead of simple concept of blocks which is present e.g. in Jinja2, you have to select the base template once in the app code, then do the weird attribute setting in the actual template while accessing it in the base template. Still you have problems if you need more than one “big” block like the page body.
Real blocks are much more elegant, and the flexibility of Templetor's “real” Python is not really necessary, while it probably can be unsafe.
精彩评论