简体   繁体   English

Django和Mustache对模板使用相同的语法

[英]Django and Mustache use the same syntax for template

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 我尝试在HTML中为mustache.js走私HTML模板,但是django模板引擎删除了应该按原样输出到前端的所有占位符

The template is included in HTML in this way: 该模板以这种方式包含在HTML中:

<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); 我可以通过运行$(el).html()来获取HTML模板,并使用Mustache.to_html(temp,data)生成html;

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. 我可以将所有模板放入另一个静态文件并从CDN提供服务,但是很难跟踪模板所属的位置,以及至少一个额外的http请求。

您只需更改标记即可:

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

You can use the {% templatetag %} templatetag to print out characters that would normally be processed by Django. 您可以使用{% templatetag %}模板标签打印出通常由Django处理的字符。 For example: 例如:

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

Results in the following in your HTML: 您的HTML中的结果如下:

{{ variable }}

For a full list of arguments see: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag 有关参数的完整列表,请参阅: https//docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

If you use django 1.5 and newer use: 如果你使用django 1.5和更新的使用:

  {% 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 ... 如果你坚持使用django 1.2 on appengine使用verbatim模板命令扩展django语法,就像这样......

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: 在您的文件(python 2.7,HDR)中使用:

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: 在你的文件(python 2.5)中使用:

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 资料来源: http//bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html

Try to use django-mustachejs 尝试使用django-mustachejs

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

Django-mustachejs will generate the following: Django-mustachejs将生成以下内容:

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

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" %}

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. 即使您不提供i18n,也可以使用trans模板标签。

You can use the built-in mustache.js set delimiter tag to change the default tags that mustache uses. 您可以使用内置的mustache.js set delimiter标记来更改mustache使用的默认标记。

ie

{{=<% %>=}}

now you can do this: 现在你可以这样做:

<% variable %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM