Skip to content

Instantly share code, notes, and snippets.

@rririanto
Created May 16, 2016 08:14
Show Gist options
  • Save rririanto/dc001763dfa27cfa06f5dea43bfd751d to your computer and use it in GitHub Desktop.
Save rririanto/dc001763dfa27cfa06f5dea43bfd751d to your computer and use it in GitHub Desktop.
Django 1.8 template block tag {% noparse %} renders template syntax characters within the block as normal text.
"""
Original Version from
dgouldin https://gist.github.com/dgouldin/634534
Modify by jimmyromanticde
Problem:
'module' object has no attribute 'module' object has no attribute 'TOKEN_TEXT'
"""
from django import template
register = template.Library()
token_formats = {
template.base.TOKEN_TEXT: '%s',
template.base.TOKEN_VAR: '%s%%s%s' % (template.base.VARIABLE_TAG_START, template.base.VARIABLE_TAG_END),
template.base.TOKEN_BLOCK: '%s%%s%s' % (template.base.BLOCK_TAG_START, template.base.BLOCK_TAG_END),
template.base.TOKEN_COMMENT: '%s%%s%s' % (template.base.COMMENT_TAG_START, template.base.COMMENT_TAG_END),
}
@register.tag
def noparse(parser, token):
token_num = 0
token = parser.tokens[token_num]
while (not (token.token_type == template.base.TOKEN_BLOCK and token.contents == 'endnoparse')):
token.contents = token_formats[token.token_type] % token.contents
token.token_type = template.base.TOKEN_TEXT
token_num += 1
token = parser.tokens[token_num]
del parser.tokens[token_num]
return NoParseNode()
class NoParseNode(template.base.Node):
def render(self, context):
return ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment