【问题标题】:How to include Handlebars.js in Twig template?如何在 Twig 模板中包含 Handlebars.js?
【发布时间】:2018-03-15 12:21:19
【问题描述】:

我同时使用 Twig 和 Handlebars.js,并且遇到了冲突。我找到了两个解决方案。一个被认为比另一个更合适,还是有第三种更合适的解决方案?如果使用我的选项 1,是否有任何推荐的命名标准将把手模板与树枝模板相关联?

选项 1 使用树枝源

my_twig_template1.html

{% block content %}
<h1>Hello</h1>
<p>{{ someTwigPhpVar }}</p>
{{ source('my_handlebars_template1.html') }}
{% endblock %}

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>

选项 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 %}

【问题讨论】:

    标签: php templates twig handlebars.js


    【解决方案1】:

    我不知道是否有一般偏好,但我觉得两者都不错。当只有几行 Handlebars 代码时,我更喜欢选项 2,而在其他情况下(包含大部分 Handlebars 代码),我更喜欢选项 1。我会将 Handlebars 文件(选项 1)放入名为 handlebars/ 的文件夹中,以便您拥有 handlebars/template1.html 等。

    另一种选择是使用变量表达式来输出变量分隔符({{}},如 documentation section about escaping 中所述)或整个 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 %}
    

    如果您只输出几个 Handlebars 变量,这会很方便,因此这将比使用单独的文件或使用 verbatim 标记更简洁。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2012-02-09
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多