简体   繁体   English

在Twig中设置默认原始过滤器

[英]Set default raw filter in Twig

I'm using Silex to build a site and Twig to display the contents based on a json file. 我正在使用Silex构建一个站点和Twig来显示基于json文件的内容。

Here's the code in the controller: 这是控制器中的代码:

$app->get('/', function() use ($app) {

    $data = $app['data']->get('contactUs', 'es');

    return $app['twig']->render('test.html', $data);

});

Data is just a custom class that takes as argument the page to be displayed and the language to use and returns an array based on the json file that Twig uses as the data on the page. Data只是一个自定义类,它将要显示的页面和要使用的语言作为参数,并根据Twig用作页面上数据的json文件返回一个数组。

The problem is that the json file contains HTML tags, and when Twig renders the page it displays them as entities, for example, my test.html template looks like this: 问题是json文件包含HTML标记,并且当Twig呈现页面时它将它们显示为实体,例如,我的test.html模板如下所示:

<!DOCTYPE html>
<html>
    <head>
        <title>Twit Test</title>
    </head>
<body>

    {{ bannerTitle }}

</body>
</html>

But that outputs the following on {{ bannerTitle }} : 但是这会在{{ bannerTitle }}上输出以下内容:

<span class='title light'>Contact Us</span>

Which by looking at the source code looks like this: 通过查看源代码看起来像这样:

&lt;span class=&#039;title light&#039;&gt;Contacto y&lt;/span&gt;&lt;br&gt;&lt;span class=&#039;title&#039;&gt;Ubicación&lt;/span&gt;

I look around at the docs, and I know I can use the raw filter on the template to avoid this, like this: 我环顾一下文档,我知道我可以在模板上使用原始过滤器来避免这种情况,如下所示:

{{ bannerTitle|raw }}

But I want to keep as clean as possible the code on the templates, and avoid putting raw to everything on the templates. 但是我希望尽可能保持模板上的代码,并避免将raw内容放在模板上的所有内容中。

Is there a way to tell Twig to treat always as raw the generated output? 有没有办法告诉Twig始终将生成的输出视为原始?

PS: I also tried parsing the generated data with htmlentities, html_entity_decode, etc with no luck :( PS:我也尝试用htmlentities,html_entity_decode等解析生成的数据,没有运气:(

I'm pretty sure this is possible by using the {% autoescape false %} {% endautoescape %} tags in twig. 我很确定这可以通过在树枝中使用{% autoescape false %} {% endautoescape %}标签来实现。

ie

{% autoescape false %}
<!DOCTYPE html>
<html>
    <head>
        <title>Twit Test</title>
    </head>
    <body>
        {{ bannerTitle }}
        {{ moreHTMLdata }}
        {{ evenMoreHTMLdata }} 
    </body>
</html>
{% endautoescape %}

More info at http://twig.sensiolabs.org/doc/tags/autoescape.html 有关更多信息,请访问http://twig.sensiolabs.org/doc/tags/autoescape.html

Failing that give {% filter raw %} {% endfilter %} a go in it's place and this should save you having to add |raw to every variable. 如果没有给出{% filter raw %} {% endfilter %} ,那么就可以节省你必须为每个变量添加|raw Using either of these methods, just remember to |escape any variables which might need it. 使用这些方法中的任何一种,只需记住|escape可能需要它的任何变量。

You shouldn't pass HTML as data to a template engine. 您不应将HTML作为数据传递给模板引擎。 If the JSON has the tags, then you will need to put |raw after every variable unfortnately. 如果JSON有标签,那么你将需要投入|raw的每一个变量后unfortnately。 Twig probably does this for security reasons. 出于安全原因,Twig可能会这样做。

Otherwise: 除此以外:

<!DOCTYPE html>
<html>
    <head>
        <title>Twit Test</title>
    </head>
<body>

    <span class='title light'>{{ bannerTitle }}</span>

</body>

Now all you need to pass in is: 现在您需要传递的是:

Array(
    'bannerTitle' => 'Contact Us'
)

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

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