21

今天我正在尝试使用 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。

我怎么解决这个问题?


更新:

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

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

4

5 回答 5

13

使用templatetag模板标签呈现括号:

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

这有点繁琐,这就是raw为 Django 1.3 提出模板标签的原因。

于 2010-10-26T08:35:14.457 回答
2

这里提到了一些解决方案:

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

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

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

于 2010-12-06T04:46:58.230 回答
0

我正在使用 Django 1.3 并添加

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

到我的 html 文件。

服务器返回:

无效的块标签:“原始”

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

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

于 2011-07-25T15:38:28.957 回答
0

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

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

于 2011-09-24T19:27:59.427 回答
0

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

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

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

于 2012-12-18T13:23:40.773 回答