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月发布,尽管在2012年底发布了Alpha / Beta版本),他们介绍了 {%逐句%}
和 {%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屋!