Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove verbatim template tag if Django >= 1.5 #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 88 additions & 86 deletions djangojs/templatetags/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import unicode_literals

from django import template
from django import VERSION
from django.contrib.staticfiles.storage import staticfiles_storage
from django.utils import six

Expand All @@ -14,92 +15,93 @@
register = template.Library()


def verbatim_tags(parser, token, endtagname):
'''
Javascript templates (jquery, handlebars.js, mustache.js) use constructs like:

::

{{if condition}} print something{{/if}}

This, of course, completely screws up Django templates,
because Django thinks {{ and }} means something.

The following code preserves {{ }} tokens.

This version of verbatim template tag allows you to use tags
like url {% url name %}. {% trans "foo" %} or {% csrf_token %} within.

Inspired by:
- Miguel Araujo: https://gist.github.com/893408
'''
text_and_nodes = []
while 1:
token = parser.tokens.pop(0)
if token.contents == endtagname:
break

if token.token_type == template.TOKEN_VAR:
text_and_nodes.append('{{')
text_and_nodes.append(token.contents)

elif token.token_type == template.TOKEN_TEXT:
text_and_nodes.append(token.contents)

elif token.token_type == template.TOKEN_BLOCK:
try:
command = token.contents.split()[0]
except IndexError:
parser.empty_block_tag(token)

try:
compile_func = parser.tags[command]
except KeyError:
parser.invalid_block_tag(token, command, None)
try:
node = compile_func(parser, token)
except template.TemplateSyntaxError as e:
if not parser.compile_function_error(token, e):
raise
text_and_nodes.append(node)

if token.token_type == template.TOKEN_VAR:
text_and_nodes.append('}}')

return text_and_nodes


class VerbatimNode(template.Node):
'''
Wrap {% verbatim %} and {% endverbatim %} around a
block of javascript template and this will try its best
to output the contents with no changes.

::

{% verbatim %}
{% trans "Your name is" %} {{first}} {{last}}
{% endverbatim %}
'''
def __init__(self, text_and_nodes):
self.text_and_nodes = text_and_nodes

def render(self, context):
output = ""
# If its text we concatenate it, otherwise it's a node and we render it
for bit in self.text_and_nodes:
if isinstance(bit, basestring):
output += bit
else:
output += bit.render(context)
return output


@register.tag
def verbatim(parser, token):
'''Renders verbatim tags'''
text_and_nodes = verbatim_tags(parser, token, 'endverbatim')
return VerbatimNode(text_and_nodes)
if VERSION < (1, 5, ):
def verbatim_tags(parser, token, endtagname):
'''
Javascript templates (jquery, handlebars.js, mustache.js) use constructs like:

::

{{if condition}} print something{{/if}}

This, of course, completely screws up Django templates,
because Django thinks {{ and }} means something.

The following code preserves {{ }} tokens.

This version of verbatim template tag allows you to use tags
like url {% url name %}. {% trans "foo" %} or {% csrf_token %} within.

Inspired by:
- Miguel Araujo: https://gist.github.com/893408
'''
text_and_nodes = []
while 1:
token = parser.tokens.pop(0)
if token.contents == endtagname:
break

if token.token_type == template.TOKEN_VAR:
text_and_nodes.append('{{')
text_and_nodes.append(token.contents)

elif token.token_type == template.TOKEN_TEXT:
text_and_nodes.append(token.contents)

elif token.token_type == template.TOKEN_BLOCK:
try:
command = token.contents.split()[0]
except IndexError:
parser.empty_block_tag(token)

try:
compile_func = parser.tags[command]
except KeyError:
parser.invalid_block_tag(token, command, None)
try:
node = compile_func(parser, token)
except template.TemplateSyntaxError as e:
if not parser.compile_function_error(token, e):
raise
text_and_nodes.append(node)

if token.token_type == template.TOKEN_VAR:
text_and_nodes.append('}}')

return text_and_nodes


class VerbatimNode(template.Node):
'''
Wrap {% verbatim %} and {% endverbatim %} around a
block of javascript template and this will try its best
to output the contents with no changes.

::

{% verbatim %}
{% trans "Your name is" %} {{first}} {{last}}
{% endverbatim %}
'''
def __init__(self, text_and_nodes):
self.text_and_nodes = text_and_nodes

def render(self, context):
output = ""
# If its text we concatenate it, otherwise it's a node and we render it
for bit in self.text_and_nodes:
if isinstance(bit, basestring):
output += bit
else:
output += bit.render(context)
return output


@register.tag
def verbatim(parser, token):
'''Renders verbatim tags'''
text_and_nodes = verbatim_tags(parser, token, 'endverbatim')
return VerbatimNode(text_and_nodes)


@register.simple_tag
Expand Down