Python Templates – Benefits and Drawbacks of XML vs Custom Syntax

djangopythontemplatesxml

I'm interested in knowing what are the real difference (benefits and drawbacks) between the two types of python templating engines; XML (like Genshi or Kid) and a custom syntax (like Cheetah or Jinja2).

I'm not looking for which is better, or a recommendation. I understand that no one solution will be perfect, and that the best solution will depend on the problem. I do want to better understand the differences between the two types before I choose one for my problem.

This list may not apply to all templating solutions.

XML Benefits:

  • Uses XML, it's mostly familial to developers. There are a few new (ifelse, flow logic) items to learn.
  • It works with existing XML toolchains.
  • It's more powerful as it is knowledgeable about the data being worked on. (Genshi is context aware)

XML Drawbacks:

Custom Syntax Benefits:

  • It's fast than XML based engines. (see earlier link)
  • It's a simple powerful syntax that should be easier to learn.

Custom Syntax Drawbacks:

  • It's another syntax to learn.
  • It might not work smoothly with existing XML toolchains.

Best Answer

I think the consideration should probably be about how the templates are going to be written. The point of templates is to make sure that people are no longer hand coding (html, LaTeX, xml, whatever). This is why templating engines allow inheritance and other fancy features.

So, Daniel Roseman has a good point when he says that getting humans to write XML is evil. To some extent, you're using templates so you don't have to do that.

With that in mind, the largest difference I've seen between python templating engines is not, in fact, whether they use XML or some custom syntax. The large difference seems to be whether or not the templating engines allow arbitrary python code to be embedded into the template.

You mentioned both Cheetah and Jinja2. Both have special syntax but Cheeta allows pretty much arbitrary python to be embeded in the template, while Jinja2 places heavy restrictions on what can be done with the template. Now you're looking at a trade off between potentially mixing business and presentation logic, with making your template clear and concise.

In the end, the benefits are going to come from how easy it is to read, write, re-read, and re-write your code.