Php 如何在细枝模板中包含handlebar.js?

Php 如何在细枝模板中包含handlebar.js?,php,templates,twig,handlebars.js,Php,Templates,Twig,Handlebars.js,我正在使用Twig和handlebar.js,并且遇到了冲突。我找到了两个解决办法。是一个比另一个更合适,还是有第三个更合适的解决方案?如果使用我的选项1,是否有任何建议的命名标准将把手模板与细枝模板关联 使用细枝源的选项1 my_twig_template1.html {% block content %} <h1>Hello</h1> <p>{{ someTwigPhpVar }}</p> {{ source('my_handlebars_te

我正在使用Twig和handlebar.js,并且遇到了冲突。我找到了两个解决办法。是一个比另一个更合适,还是有第三个更合适的解决方案?如果使用我的选项1,是否有任何建议的命名标准将把手模板与细枝模板关联

使用细枝源的选项1

my_twig_template1.html

{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
{{ source('my_handlebars_template1.html') }}
{% endblock %}
<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>
{%block content%}
你好
{{someTwigPhpVar}}

{{source('my_handlebar_template1.html')} {%endblock%}
my_Handlebar_template1.html

{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
{{ source('my_handlebars_template1.html') }}
{% endblock %}
<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>

{{someHandleBarVariable}}
{{someHandleBarVariable}}
选项2使用细枝逐字记录

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 %}
{%block content%}
你好
{{someTwigPhpVar}}

{%verbatim%} {{someHandleBarVariable}} {{someHandleBarVariable}} {%endverbatim%} {%endblock%}
我不知道是否有普遍的偏好,但我觉得两者都不错。当车把代码只有几行时,我更喜欢选项2,在其他情况下,我更喜欢选项1(车把代码的大部分)。我会将Handlebar文件(选项1)放入名为
handlebar/
的文件夹中,这样您就有了
handlebar/template1.html

另一个选项是使用变量表达式输出变量分隔符(
{{
}
,如中所述)或整个把手表达式:

{% 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 %}
{%block content%}
你好
{{someTwigPhpVar}}

{{{}someHandleBarVariable{{{}} {{{{someHandleBarVariable}}}} {%endblock%}
如果您只输出几个Handlebar变量,这将非常方便,因此这将比使用单独的文件或使用
逐字
标记更简洁