Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Django templates not to parse a block containing code that looks like template tags?

I've got some html files that include templates to be used by jQuery.tmpl. Some tmpl tags (like {{if...}}) look like Django template tags and cause a TemplateSyntaxError. Is there a way I can specify the Django template system should ignore a few lines and output them exactly as they are?

like image 877
Jake Avatar asked Nov 17 '11 00:11

Jake


People also ask

What is Django template tags?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client. template.html : <ul> {% for x in mymembers %} <li>{{ x. firstname }}</li> {% endfor %} </ul> Run Example »

How do I use custom template tags in Django?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

What does the built in Django template tag Lorem do?

lorem. Displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates. A number (or variable) containing the number of paragraphs or words to generate (default is 1).

What is the name of the template tag that is used to define which child blocks can be filled in by the child templates?

html , defines an HTML skeleton document that you might use for a two-column page. It's the job of “child” templates to fill the empty blocks with content. In this example, the block tag defines three blocks that child templates can fill in.


2 Answers

As of Django 1.5, this is now handled by the built-in verbatim template tag.

In older versions of Django, the built-in way would be to manually escape each template item with the templatetag template tag ( https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#templatetag ), but I suspect that that's not what you want to do.

What you really want is a way to mark a whole block as raw (rather than interpretable) text, which requires a new custom tag. You might want to check out the raw tag here: http://www.holovaty.com/writing/django-two-phased-rendering/

like image 167
Michael C. O'Connor Avatar answered Oct 12 '22 14:10

Michael C. O'Connor


There are a couple open ticket to address this issue: https://code.djangoproject.com/ticket/14502 and https://code.djangoproject.com/ticket/16318 You can find a proposed new template tag verbatim below:

"""
From https://gist.github.com/1313862
"""

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))
like image 40
Mark Lavin Avatar answered Oct 12 '22 12:10

Mark Lavin