views:

281

answers:

2

For a new project we're writing documentation about the Django template system. We use Django for the documentation project itself too, so Django picks up all our example variables in the sample code and tries to render them. The only way we found to get around this is to use {% templatetag %}, but that makes our code really unreadable. Is there maybe a way to make Django ignore all template variables in a specific section?

+4  A: 

Due to limitations in the Django template lexer (like being a kludgy hack), this is impossible. However, if you are willing to put your example code in separate files, you can use the ssi tag:

{% ssi /path/to/my/code/examples/example01.html %}

And it won't parse the file, just include it verbatim. However, this also has limitations in that you can't use variables in the include path (i.e. if you move template locations, you have to rewrite or at least find-and-replace your template files), and you have to put the include path (i.e. /path/to/my/code/examples) in the ALLOWED_INCLUDE_ROOTS setting in your settings.py. (See http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ssi.)

LeafStorm
+1 for "like being a kludgy hack".
Ignacio Vazquez-Abrams
Ha, our initial solution was to do this with ajax, or use "[" instead of "{" and replace them back with javascript. If no one else will come up with something better, I guess you answered it.
Koen Bok
I like your "'[' and swap with JS" idea better.
lawrence
+2  A: 

A possible solution is to write the templates as usual (with {{ x }}), but save them as .txt (or any other extension you want). Write a script the runs over these files, and automatically creates the .html for you, by doing the reverse of templatetag (replacing {{ with {% templatetag openvariable %}etc). Make sure the code runs after you update the templates.

Ofri Raviv