3

I'm working on a website which uses Django templates, and I have to produce HTML which contains paired curly quotes. Is there any way to disable Django's tag processing for a block of code? Something like a literal block tag would be nice, so that:

{% literal %}
 {% LITERAL {{ BRACES }}
{% endliteral %}

... would produce {% LITERAL {{ BRACES }} in the output.

Chris B.
  • 85,731
  • 25
  • 98
  • 139

3 Answers3

5

For the record, this is possible now with the template tag verbatim.

sanfilippopablo
  • 1,449
  • 2
  • 15
  • 19
  • Here's a link to the documentation for the `verbatim` tag: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#verbatim – TheRightChoyce Aug 06 '14 at 22:21
2

EDIT: Your syntax is currently impossible with the current lexer / parser system.

Why? Basically the template system has a Lexer and a Parser. The Lexer takes the template string as input, and tokenizes it. The parser then takes the list of tokens in its constructor and parses them into a list of a bunch of Nodes for the compiled template. The template tags and filters only have access to the already constructed parser -- you can't access the initial lexer string. See the comments in django/templates/__init__.py

However, there is a solution. It's not mine (see below), but its to basically use server side includes {% ssi some_file.html %} to include an extra file that has the literal text. Yes this is an ugly solution; but without a major rewrite of the templating system it will have to suffice.

Easy Way to Escape Django Template Variables

Community
  • 1
  • 1
dr jimbob
  • 17,259
  • 7
  • 59
  • 81
0

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • Useless. You're seriously suggesting I should write the above as '{% templatetag openblock %} LITERAL {% templatetag openvariable %} BRACES {% templatetag closevariable %}'? A block could conceivably contain hundreds of brace pairs. – Chris B. Jul 26 '11 at 22:08