Thursday, May 27, 2010

templating engine cookoff updated

Ian wished for loop syntax examples in the Templating Engine Cookoff, so I just added them.

And none of you Django programmers have given me code to include Django templates in the cookoff. You can't throw an exception in this town without hitting a Django programmer, so what's up? C'mon, it's easy - I just need something analogous to this:

The answer to the Ultimate
Question is ${everything['universe'].answer}.

Among our weapons are:
% for weapon in weapons:
${weapon}
% endfor

import mako
import mako.template
class MakoDemo(Demo):
name = 'mako'
version = mako.__version__
def demo(self, vars):
self.template_name = 'mako.txt'
template = mako.template.Template(filename=self.template_name)
return template.render(**vars)


[EDIT: Well done, Django folks! Django is in the cookoff now. Except... hmm... it reacts to missing variables by leaving blanks instead of throwing an exception... so I need a new trick if I want to actually see the Django error stack.]

6 comments:

Xubuntix said...

Hi, here is a django version (I hope it works...)

http://pastebin.com/YUq32sTz

Xubuntix said...

...and if you need to separate template and code, here is the obvious change...

http://pastebin.com/J7qF7g1G

Simon said...

I've posted a solution here: http://gist.github.com/415785

ninternity© said...

The template:
http://paste.pocoo.org/show/218880/

The code:
http://paste.pocoo.org/show/218887/

And the output:
http://paste.pocoo.org/show/218888/

Paul said...

Here's my Django entry. It's a 2 file gist. The error output is pretty worthless by design though. Template debugging is mostly useful when the full django stack is in use.

Thanks Catherine!

http://gist.github.com/415973

Xubuntix said...

Hi, seeing the output of the django version brings up mixed feelings for me:
on the one hand, that is documented behavior and part of the django philosophy: "Template authors should not brake the application". On the other hand you want to see the error messages during testing/debugging.

In django this is done normally with the settings module. There you would clearly distinguish between production and development cases. And you can (of cause) set the missing value errors to show up.
I have done this in a updated version

http://pastebin.com/hWygByN6

where the missing variable error is explicitly raised.

One other thing with your test is that only one sort of error is tested. Django shows a different behavior, if a syntax error is found in the template. This is the second string in the posted code.