Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.
share|improve this question
    
XML syntax for a DSL isn't XML. It is limited XML, and that means the reader still has to learn funny rules; you can't just write any set of nested tags. It is also than a conventional DSL in that it uses clunky syntax with funny rules (try typing "&" into an XML DSL) IMHO. –  Ira Baxter Jul 8 '11 at 17:35
    
I think the XML syntax engines support XML but extend it in an XML idiomatic way for added functionality of templating. So while it's not not a pure syntax it's still familial to developers with XML experience. –  Supermighty Jul 11 '11 at 12:07
add comment

migrated from stackoverflow.com Jul 11 '11 at 11:15

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
That's a good point. It hadn't occurred to me to see enforcing separation of logic and presentation as a feature. –  Supermighty Jul 8 '11 at 17:43
    
I hope it's helpful. Welcome to StackOverflow by the way. It's customary here to "upvote" helpful answers by clicking on the arrow above the number. Also, if someone answers your question to your satisfaction, you can "accept" the answer by clicking on the check mark below the number. Also, note that you can vote on interesting questions and answers even if they aren't your own, a practice that is highly encouraged. Best of luck. –  Wilduck Jul 8 '11 at 18:41
add comment

The one overriding principle: getting humans to write XML is evil.

share|improve this answer
3  
I can't upvote this enough... –  Paul Woolcock Jul 8 '11 at 13:51
    
Just because XML might be evil doesn't mean it isn't the best solution. It would be perfect if you were integrating with an existing XML workflow. –  Supermighty Jul 8 '11 at 14:12
add comment

I think, your Pros/Cons are problematic

XML: It's slow.

thats not neccessarily true. Like in Jinja, you could compile a XML-Template. Maybe genshi already does that.

XML: it's familial to developers

also not true. how to do a loop in XML? How to extend a base Template? Depends on the template, obviously. So, you still have to learn, and the XML-Syntax doesnt help.

Custom: It's another syntax to learn.

as said above, you have to learn a new syntax anyway, and with custom languages, the syntax may be a little more comforting, for example with 'else'-statements. also keep in mind that not only programmers, but designers with a less technical point of view may have to use the template.

Custom: It might not work smoothly with existing toolchains.

That may be true for XML as well: you probably want HTML as an output, which is (when you dont go for XHTML) no XML. But your point is true, and for me it's the answer: the different approaches fit different needs/toolchains.

The needs and toolchains are imho mostly determined by the expected output. If you have to output valid XML, use XML-Templating, if not, dont.

share|improve this answer
    
For myself personally I'll output HTML, so the toolchain point isn't really directed at me. –  Supermighty Jul 11 '11 at 11:55
    
Thanks for your answer, I've updated my pros/cons with your feedback. –  Supermighty Jul 11 '11 at 12:01
    
@Supermighty: if you output HTML, you have tools (like syntax highlighting) that, in my experience, will play better with non-xml-templates –  keppla Jul 11 '11 at 12:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.