简体   繁体   English

原始或逃生包含在树枝中

[英]raw or escape on include in twig

I would like to escape a file, which I'm including 我想转义一个文件,其中包括

following code won't escape the html tags in file "_custom_plugin_script.html.twig". 以下代码不会转义文件“ _custom_plugin_script.html.twig”中的html标签。 Is there another way? 还有另一种方法吗?

<pre>
    {% autoescape true %}
        {% include "_custom_plugin_script.html.twig" | raw %}
    {% endautoescape %}
</pre>

After a couple days, I have found a workaround, but not an answer. 几天后,我找到了解决方法,但没有找到答案。 So first raw would not escape therefore I should use escape. 因此,首先未处理不会逃脱,因此我应该使用逃逸。 However raw and escape won't work within {% %} but in {{}}. 但是,原始和转义在{%%}中无效,而在{{}}中无效。

So here comes the workaround 所以这就是解决方法

Content of the Action 行动内容

$customPluginScript = $app['twig']->render('_custom_plugin_script.html.twig', array(
    'data' => $data,
));


return $app['twig']->render('confirm.html.twig', array(
    'data' => $data,
    'customPluginScript' => $customPluginScript
));

And the a part of confirm.html.twig 和一部分confirm.html.twig

<script>
// don't escape content of customPluginScript
  {{ customPluginScript | raw }}
</script>


<!-- escape content of customPluginScript -->
<pre>
  {{ customPluginScript }}
</pre>
{% filter escape %}
    {% include '...' %}
{% endfilter %}

See the docs for details . 有关详细信息,请参阅文档。

As this is the first result that comes up when googling for twig include raw it's worth mentioning that twig now supports this with the following syntax 因为这是谷歌搜索twig include raw时出现的第一个结果,所以值得一提的是twig现在支持以下语法

{{ source('AcmeSomeBundle:Default:_custom_plugin_script.html.twig') }}

However, this does not render the template as mentioned by barius. 但是,这不会如barius所述呈现模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM