如何在树枝模板中混合解释和未解释的文本

How to include mix of interpreted and uninterpreted text in twig template

我将 twig 与 Symfony2 框架一起使用。 我还使用 HTML 中嵌入的小胡子模板进行客户端渲染(在 javascript 中)。

在我的 Symfony 项目中,我正在构建带有其他 twig 模板块的 twig 模板。 "include" 功能让这一切变得简单。

当我想在 twig 模板中包含一些小胡子模板时,我可以使用 "source" 功能,该功能相当于整个模板的 "verbatim" 功能。

当我想混合解释和未解释的文本时出现问题

例如,我希望解释路径,但不希望解释小胡子标记。

my_twig_template.html.twig

<body> 
    blablabla
    {{ source('my_mixed_template.html.twig') }}
</body>

my_mixed_template.html.twig

<script type="x-tmpl-mustache" id="my-mixed-template">

    {{ path('path_to_be_interpreted_by_twig') }}

    {{mustache_var_not_to_be_interpreted_by_twig}}    

</script>

知道怎么做吗?

最后我自己找到了解决办法

在主树枝模板中:

{% include 'my_mustache_template.html.twig' %}

在嵌入小胡子模板的树枝模板中:

{% verbatim %}
    <script type="x-tmpl-mustache" id="my-id">
        {{ mustache_variable }}
        {% endverbatim %} {{ twig_variable }} {% verbatim %}
        {{ another_mustache_variable }}
    </script>
{% endverbatim %}

{% endverbatim %} {{ twig_variable }} {% verbatim %} 序列允许 twig 解释器用实际值替换 twig 变量。