Django and Mustache use the same syntax for template

浪子不回头ぞ 提交于 2019-12-20 09:49:00

问题


I try to smuggle HTML template in the HTML for mustache.js, however the django template engine remove all the placeholders that should be output as-is to the front-end

The template is included in HTML in this way:

<script type="text/x-mustache-template" data-id="header_user_info">
    <div id="header_user_info">
        <div id="notification">0</div>
        <a href="#">{{username}}</a>
    </div>
</script>

and I can get the HTML template by running $(el).html(), and generate html by using Mustache.to_html(temp, data);

I could put all the template into another static file and serve from CDN, but then it would be hard to track where the template belongs, and at least one extra http request.


回答1:


You can use the {% templatetag %} templatetag to print out characters that would normally be processed by Django. For example:

{% templatetag openvariable %} variable {% templatetag closevariable %}

Results in the following in your HTML:

{{ variable }}

For a full list of arguments see: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag




回答2:


You can simply change the tags:

Mustache.tags = ['[[', ']]'];



回答3:


If you use django 1.5 and newer use:

  {% verbatim %}
    {{if dying}}Still alive.{{/if}}
  {% endverbatim %}

If you are stuck with django 1.2 on appengine extend the django syntax with the verbatim template command like this ...

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))

In your file (python 2.7, HDR) use:

from django.template import Context, Template
import django
django.template.add_to_builtins('utilities.verbatim_template_tag')

html = Template(blob).render(Context(kwdict))

In your file (python 2.5) use:

from google.appengine.ext.webapp import template
template.register_template_library('utilities.verbatim_template_tag')

Source: http://bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html




回答4:


Try to use django-mustachejs

{% load mustachejs %}
{% mustachejs "main" %}

Django-mustachejs will generate the following:

<script>Mustache.TEMPLATES=Mustache.TEMPLATES||{};Mustache.TEMPLATES['main']='<<Your template >>';</script>



回答5:


I have the same issue, but using

{% templatetag openvariable %} variable {% templatetag closevariable %}

is too verbose for me. I've just added a very simple custom template tag:

@register.simple_tag
def mtag(tagContent):
    return "{{%s}}" % tagContent

So that I can now write:

{% mtag "variable" %}



回答6:


You can use the built-in mustache.js set delimiter tag to change the default tags that mustache uses.

i.e.

{{=<% %>=}}

now you can do this:

<% variable %>



回答7:


I have the same issue, so most of the time my variables are part of a translatable string.

{% trans "The ball is {{ color }}" %}

You can use the trans templatetag even if you don't offer i18n.



来源:https://stackoverflow.com/questions/7985594/django-and-mustache-use-the-same-syntax-for-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!