Created
April 16, 2012 09:33
-
-
Save akx/2397269 to your computer and use it in GitHub Desktop.
Django verbatim templatetag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment