【问题标题】:jquery template tags conflict with Django template!jquery 模板标签与 Django 模板冲突!
【发布时间】:2011-04-30 15:07:57
【问题描述】:

今天我正在尝试使用 jquery-tmpl {{if}} 和 {{else}} 语句。

<script id="mission-dialog" type="text/x-jquery-tmpl">
    <h3>${name}</h3>
    <p>${description}</p>
    <ul>
        {{each(i,cond) conditions.data}}
        <li>
            <img src="${cond.image}"/>
            <h4>${cond.name}</h4>
            <p class="status">${cond.status.value}/${cond.status.max}</p>
        </li>
        {{/each}}
    </ul>
</script>

但如您所知,{{ }} 也是为 django 模板保留的。 所以django会发出无法解析的TemplateSyntaxError。

我该如何解决这个问题?


更新:

我从这里找到了一个有效的 自定义标签 (GPL) 实现。

http://www.holovaty.com/writing/django-two-phased-rendering/

【问题讨论】:

    标签: jquery django django-templates jquery-templates


    【解决方案1】:

    使用templatetag模板标签来渲染括号:

    {% templatetag openvariable %}each(i,cond) conditions.data{% templatetag closevariable %}
    

    这有点繁琐,这就是为什么 Django 1.3 建议使用 raw 模板标签。

    【讨论】:

    • 谢谢丹尼尔!不过你说的太浮夸了。我应该等待 Django 1.3 ... :)
    • 或者将 jquery-template 代码不在 html 中,而是在代码中的某个位置,并将其传递给模板.. :(
    • 在我的用例中,客户端 CMS 正在我的 js 文件中应用 django 模板。冲突实际上与 jQuery 模板引擎本身的声明一样深。
    【解决方案2】:

    这里提到了几个解决方案:

    https://github.com/nje/jquery-tmpl/issues#issue/17 - 编辑:旧仓库

    https://github.com/jquery/jquery-tmpl/issues/#issue/74

    我最喜欢的是{% verbatim %} 模板标签,它允许您从 Django 模板中构建 jQuery 模板。

    【讨论】:

      【解决方案3】:

      我正在使用 Django 1.3 并添加

      {% raw %} this should be ignored by django's template framework {% endraw %}
      

      到我的 html 文件。

      服务器返回:

      无效的块标签:'raw'

      此外,在文档中,我似乎无法找到有关您正在谈论的此标签的信息。

      https://docs.djangoproject.com/en/1.3/ref/templates/builtins/

      【讨论】:

      【解决方案4】:

      这里有很多关于使用 Django 模板、使用 icanhaz 的信息,但我认为这与 JQuery 模板足够相似。

      http://tothinkornottothink.com/post/4282971041/using-jquery-templating-icanhaz-js-with-django

      【讨论】:

        【解决方案5】:

        如果不支持“逐字”标签,我发现默认值很有用。 看起来像这样:

        {{ some_undefined_variable|default: '....here goes the subject template....'}}
        

        PS。 ChicagoBoss 用户的注意事项,它在 ErlyDTL 中不支持“逐字”标签。

        【讨论】:

          猜你喜欢
          • 2020-04-02
          • 2012-01-08
          • 2016-09-02
          • 2014-11-27
          • 2019-12-01
          • 2015-12-08
          相关资源
          最近更新 更多