在django模板中轻松嵌入handlebars.js模板
django-templatetag-handlebars的Python项目详细描述
django templatetag把手允许您嵌入Handlebars.js Django模板中的模板。
大多数模板标记代码来自Miguel Araujo’s verbatim snippet。
安装
pip install django-templatetag-handlebars
用法
- 将templatetag_handlebars添加到INSTALLED_APPS
- 添加HTML标题:
{% load templatetag_handlebars %} <head> {% handlebars_js %} ... </head>
- 调用模板标记,并编写把手模板:
{% tplhandlebars "tpl-infos" %} {{total}} {% trans "result(s)." %} <p>{% trans "Min" %}: {{min}}</p> <p>{% trans "Max" %}: {{max}}</p> {% endtplhandlebars %}
- 在您的页面中出现以下块:
<script type="text/x-handlebars" id="tpl-infos"> {{total}} result(s). <p>Min: {{min}}</p> <p>Max: {{max}}</p> <script>
- 像往常一样,使用Handlebars.jsapi:
var properties = { total: 10, min: 4, max: 5 }; var template = Handlebars.compile($('#tpl-infos').html()), rendered = template(properties);
- 您的渲染字符串已准备就绪,正在等待插入您的dom:)
10 result(s). <p>Min: 4</p> <p>Max: 5</p>
高级
可以使用{% verbatim %}标记转义特定部分。为了 例如,您可能希望handlebars模板的子部分 由Django呈现:
<script type="text/x-handlebars" id="tpl-django-form"> <form> {% verbatim %} {{#if id}}<h1>{{first}} {{last}}</h1>{{/if}} {% trans "Your id is" %} {{ id }} {% endverbatim %} {{ yourform.as_p }} </form> </script>
使用ember.js
在settings.py中,确保将以下属性设置为True。这是因为Ember.js需要一个稍有不同的脚本id声明
USE_EMBER_STYLE_ATTRS = True
脚本块将呈现为`<script type="text/x-handlebars"data-template-name="%s">`。
许可证
- Lesser GNU Public License
- ^{tt3}$ is released under the MIT license - Copyright 2011 Yehuda Katz
更改日志
1.3.1(2016-01-04)
- 添加python3兼容性(感谢tadas dailyda)
1.3.0(2015-05-26)
- 添加django 1.8兼容性(感谢@markvl)
1.2.0(2012-05-30)
- 使用设置添加与ember.js的兼容性(默认为false)。请参阅自述。
1.1.0(2012-03-26)
- 使用模板标记移除对模板鸡蛋加载器的依赖关系
1.0.1(2012-03-16)
- 固定包装
1.0.0(2012-03-16)
- 初始工作版本