Skip to content

Instantly share code, notes, and snippets.

Forked from ericflo/
Created October 25, 2011 19:03
Show Gist options
  • Save paulsmith/1313862 to your computer and use it in GitHub Desktop.
Save paulsmith/1313862 to your computer and use it in GitHub Desktop.
verbatim Django template tag
jQuery templates use constructs like:
{{if condition}} print something{{/if}}
This, of course, completely screws up Django templates,
because Django thinks {{ and }} mean something.
Wrap {% verbatim %} and {% endverbatim %} around those
blocks of jQuery templates and this will try its best
to output the contents with no changes.
from django import template
register = template.Library()
class VerbatimNode(template.Node):
def __init__(self, text):
self.text = text
def render(self, context):
return self.text
def verbatim(parser, token):
text = []
while 1:
token = parser.tokens.pop(0)
if token.contents == 'endverbatim':
if token.token_type == template.TOKEN_VAR:
elif token.token_type == template.TOKEN_BLOCK:
if token.token_type == template.TOKEN_VAR:
elif token.token_type == template.TOKEN_BLOCK:
return VerbatimNode(''.join(text))
Copy link

jshaw commented Oct 12, 2012

So useful, thanks!

Copy link

max-b commented Jan 20, 2013

Super helpful!

Copy link

c0x6a commented Feb 13, 2013

Just what I need... thank you!

Copy link

dzen commented Feb 19, 2013

Copy link

Thanks this solves it for django 1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment