Django 模板中的 Handlebars.js [英] Handlebars.js in Django templates
问题描述
我需要一个 javascript 模板系统,我认为 handlebars.js 在这种情况下做得很好.我在 django 模板中与把手模板存在语法冲突,因为 django 尝试呈现把手变量.
I need a javascript templating system and i think handlebars.js does an excellent job in this case. I'm having syntax conflicts with handlebars templates inside a django template because django tries to render handlebars variables.
django 模板中是否有一个标签可以停止渲染带有花括号的块?
Is there a tag in django templates to stop rendering a block with curly braces?
类似于:
{{ django_context_varable }} #works
{{% raw %}}
<script id="restaurants-tpl" type="text/x-handlebars-template">
<ul>
{{#restaurants}} #not rendered by django, plain text
<li>{{name}}</li>
{{/restaurants}}
</ul>
</script>
{{% endraw %}}
<小时>
编辑
可能我发现了这个.它工作正常.
Likely i found this. It works fine.
更新
Django 1.5 本身支持 verbatim 标签.
Django 1.5 supports verbatim tag natively.
推荐答案
django 模板中是否有一个标签可以停止渲染带有花括号的块?
Is there a tag in django templates to stop rendering a block with curly braces?
Django 1.0-1.4 的旧答案: 不, 尽管您可以将块放在一个单独的文件中并包含它而不渲染或使用不同的模板引擎.
OLD Answer for Django 1.0-1.4: No, though you could though you could put the block in a separate file and include it without rendering or use a different templating engine.
新答案:上述答案在 2011 年 8 月提出并回答问题时是正确的.从 Django 1.5(2013 年 2 月发布,虽然 alpha/beta 版本在 2012 年底)开始,他们引入了 {% verbatim %}
和 {% endverbatim %}
django 模板引擎处理块中的内容.
New Answer: The answer above was correct in August 2011 when the question was asked and answered. Starting in Django 1.5 (released Feb 2013, though alpha/beta versions in late 2012), they introduced the {% verbatim %}
and {% endverbatim %}
which will prevent the django template engine from processing the content in the block.
因此,对于提出的问题,以下内容将在 django 1.5+ 中开箱即用:
So for the question asked the following will work in django 1.5+ out of the box:
{{ django_context_varable }} #works
{% verbatim %}
<script id="restaurants-tpl" type="text/x-handlebars-template">
<ul>
{{#restaurants}} #not rendered by django, plain text
<li>{{name}}</li>
{{/restaurants}}
</ul>
</script>
{% endverbatim %}
关于逐字记录的文档在这里.是的,其他人早些时候注意到了这一点,但由于这是公认的答案,我应该列出最简单的解决方案.
The documentation on verbatim is here. Yes, this was noted by others earlier, but as this is the accepted answer I should list the easiest solution.
这篇关于Django 模板中的 Handlebars.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!