Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@register.tag
def verbatim(parser, token):
text = []
while 1:
token = parser.tokens.pop(0)
if token.contents == 'endverbatim':
break
if token.token_type == template.TOKEN_VAR:
text.append('{{')
elif token.token_type == template.TOKEN_BLOCK:
text.append('{%')
text.append(token.contents)
if token.token_type == template.TOKEN_VAR:
text.append('}}')
elif token.token_type == template.TOKEN_BLOCK:
text.append('%}')
return VerbatimNode(''.join(text))
@jshaw

This comment has been minimized.

Show comment
Hide comment
@jshaw

jshaw Oct 12, 2012

So useful, thanks!

jshaw commented Oct 12, 2012

So useful, thanks!

@max-b

This comment has been minimized.

Show comment
Hide comment
@max-b

max-b Jan 20, 2013

Super helpful!

max-b commented Jan 20, 2013

Super helpful!

@cjdp25

This comment has been minimized.

Show comment
Hide comment
@cjdp25

cjdp25 Feb 13, 2013

Just what I need... thank you!

cjdp25 commented Feb 13, 2013

Just what I need... thank you!

@dzen

This comment has been minimized.

Show comment
Hide comment
@Ahmad-Dukhan

This comment has been minimized.

Show comment
Hide comment
@Ahmad-Dukhan

Ahmad-Dukhan May 12, 2013

Thanks this solves it for django 1.4

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