【问题标题】:Django and Mustache use the same syntax for templateDjango 和 Mustache 使用相同的模板语法
【发布时间】:2011-12-20 14:22:17
【问题描述】:

我尝试在 HTML 中为 mustache.js 走私 HTML 模板,但是 django 模板引擎删除所有应该是的占位符 原样输出到前端

模板以这种方式包含在 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>

我可以通过运行 $(el).html() 来获取 HTML 模板,并生成 使用 Mustache.to_html(temp, data);

我可以将所有模板放入另一个静态文件并从 CDN,但是很难跟踪模板所属的位置, 以及至少一个额外的 http 请求。

【问题讨论】:

  • @Alasdair 查看我的答案以获得更好的方法。 templatetag 解决方案太冗长了。
  • 您可以在项目中使用逐字标记。看this link

标签: javascript django django-templates javascript-framework mustache


【解决方案1】:

您可以使用内置的 mustache.js 设置分隔符标记来更改 mustache 使用的默认标记。

{{=<% %>=}}

现在你可以这样做了:

<% variable %>

【讨论】:

    【解决方案2】:

    如果您使用 django 1.5 及更新版本:

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

    如果您在 appengine 上被 django 1.2 卡住,请使用这样的逐字模板命令扩展 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))
    

    在您的文件(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))
    

    在您的文件(python 2.5)中使用:

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

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

    【讨论】:

      【解决方案3】:

      您可以简单地更改标签:

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

      【讨论】:

      • 更改 Mustache 标签以处理 Django 问题?不理想。过去没有太多选择,但 Django 1.5 现在支持逐字标记。请参阅下面@cat 的回答。
      • 为什么逐字标记更好?
      • 刚刚使用这种方法来避免 Mustache 和 Liquid 模板冲突。对我来说最简单、最干净的解决方案。
      • 我应该把这个放在哪里?
      • @cafebabe1991 您想在 Mustache 脚本加载后立即执行此操作。因此,如果您使用 jQuery,您应该可以在文件的底部/顶部执行 &lt;script&gt;$(function() { Mustache.tags = ...; })&lt;/script&gt;
      【解决方案4】:

      我也有同样的问题,所以大多数时候我的变量都是可翻译字符串的一部分。

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

      即使您不提供 i18n,您也可以使用 trans 模板标签。

      【讨论】:

        【解决方案5】:

        我有同样的问题,但使用

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

        对我来说太冗长了。我刚刚添加了一个非常简单的自定义模板标签:

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

        所以我现在可以写了:

        {% mtag "variable" %}
        

        【讨论】:

          【解决方案6】:

          尝试使用django-mustachejs

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

          Django-mustachejs 将生成以下内容:

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

          【讨论】:

            【解决方案7】:

            您可以使用{% templatetag %} 模板标签打印出通常由 Django 处理的字符。例如:

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

            在您的 HTML 中产生以下结果:

            {{ variable }}
            

            有关参数的完整列表,请参阅:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

            【讨论】:

            • 太棒了。然后接受我的回答。您可能也应该接受您提出的其他问题的答案。
            • 不错的一个!不知道这个标签。
            • 虽然这行得通,但肯定有比用{% templatetag openvariable %} 替换{{ 的每个实例更好的方法。
            • 包含文本而不将其解析为 django 模板有一个很好的要点:gist.github.com/629508。它允许您将部分包装在 {% verbatim %}{{ unrendered }}{% endverbatim %}
            • 在下面查看我的答案以获得更好的方法。
            猜你喜欢
            • 2021-01-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-09
            • 1970-01-01
            • 1970-01-01
            • 2012-05-02
            • 1970-01-01
            相关资源
            最近更新 更多