[英]How to include Handlebars.js in Twig template?
I am using both Twig and Handlebars.js, and was running into conflicts. 我同时使用Twig和Handlebars.js,并且遇到了冲突。 I found two solutions.
我找到了两种解决方案。 Is one considered more proper than the other, or is there a third more appropriate solution?
是一种被认为比另一种更合适,还是存在第三种更合适的解决方案? If my Option 1 is used, are there any recommended naming standards to associate the handlebars template to the twig template?
如果使用我的选件1,是否有建议的命名标准将把手模板与树枝模板相关联?
Option 1 using twig source 选项1使用树枝来源
my_twig_template1.html my_twig_template1.html
{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
{{ source('my_handlebars_template1.html') }}
{% endblock %}
my_handlebars_template1.html my_handlebars_template1.html
<script id="my-handlebars-item-1" type="text/x-handlebars-template">
{{someHandleBarVariable}}
</script>
<script id="my-handlebars-item-2" type="text/x-handlebars-template">
{{someHandleBarVariable}}
</script>
Option 2 using twig verbatim 选项2使用细枝逐字记录
my_twig_template2.html my_twig_template2.html
{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
{% verbatim %}
<script id="my-handlebars-item-1" type="text/x-handlebars-template">
{{someHandleBarVariable}}
</script>
<script id="my-handlebars-item-2" type="text/x-handlebars-template">
{{someHandleBarVariable}}
</script>
{% endverbatim %}
{% endblock %}
I don't know if there's a general preference, but both look okay to me. 我不知道是否有一般偏好,但是我都觉得还可以。 I would prefer option 2 when there's just a few lines of Handlebars code and option 1 in other cases (with big sections of Handlebars code).
当只有几行Handlebars代码时,我会选择选项2;而在其他情况下(带有很大一部分Handlebars代码),我会选择Option 1。 I would put the Handlebars files (option 1) into a folder called
handlebars/
, so that you have handlebars/template1.html
etc. 我会将Handlebars文件(选项1)放入一个名为
handlebars/
的文件夹中,以便拥有handlebars/template1.html
等。
Another option is to use a variable expression to output the variable delimiters ( {{
and }}
, as described in the documentation section about escaping ) or the whole Handlebars expression: 另一种选择是使用变量表达式输出变量定界符(
{{
和}}
,如有关转义的文档部分所述)或整个Handlebars表达式:
{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
<script id="my-handlebars-item-1" type="text/x-handlebars-template">
{{ '{{' }} someHandleBarVariable {{ '}}' }}
</script>
<script id="my-handlebars-item-2" type="text/x-handlebars-template">
{{ '{{someHandleBarVariable}}' }}
</script>
{% endblock %}
This is handy if you are outputting just a couple of Handlebars variables so this will be more concise than having a separate file or using the verbatim
tag. 如果仅输出几个Handlebars变量,这将很方便,因此与拥有单独的文件或使用
verbatim
标记相比,这将更加简洁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.