在 Twig 中进行 Unescape 或 html 解码(PHP 模板)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15453579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unescape or html decode in Twig (PHP Templating)
提问by Eric Herlitz
I'm using twig 1.12.2. My code generates some elements from code-behind, when rendering these with the latest version of twig they get html-encoded
我正在使用树枝 1.12.2。我的代码从代码隐藏生成一些元素,当使用最新版本的树枝渲染这些元素时,它们会得到 html 编码
{% for item in files_folders %}
<tr class="{{ cycle(['tr_odd', 'tr_even'], loop.index) }}">
<td><img src="../templates/images/sharepoint/{{ item.ContentType }}.gif" border="0" alt=""/></td>
<td>{{ item.Link }}</td>
<td>{{ item.Modified }}</td>
<td>{{ item.FileSize }}</td>
<td>{{ item.FileType }}</td>
</tr>
{% endfor %}
This will output this
这将输出这个
<tr class="tr_even">
<td><img src="../templates/images/sharepoint/Document.gif" border="0" alt=""/></td>
<td><a href='?download=/ddd.png'>ddd.png</a></td>
<td>2013-03-04 17:47:38</td>
<td>64.8 KB</td>
<td>png</td>
</tr>
<tr class="tr_odd">
<td><img src="../templates/images/sharepoint/Document.gif" border="0" alt=""/></td>
<td><a href='?download=/asdasd.png'>asdasd.png</a></td>
<td>2013-03-03 20:01:52</td>
<td>66.04 KB</td>
<td>png</td>
</tr>
When I debug and have a look at the data before it's sent to twig it is not escaped. I haven't found any alternative to {{ item.Link }} to render data as-is.
当我在将数据发送到树枝之前调试并查看数据时,它不会被转义。我还没有找到任何替代 {{ item.Link }} 来按原样呈现数据的方法。
Thanks
谢谢
回答by romainberger
You can use the rawfilter to make twig render raw html
您可以使用raw过滤器使树枝呈现原始 html
http://twig.sensiolabs.org/doc/filters/raw.html
http://twig.sensiolabs.org/doc/filters/raw.html
{% autoescape %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
回答by PR Whitehead
You should be careful with using |raw. Saying that the data is safe, means you are trusting it 100%.
使用 |raw 时应该小心。说数据是安全的,意味着您 100% 信任它。
Personally I would suggest using a custom twig filter:
我个人建议使用自定义树枝过滤器:
class CustomExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('unescape', array($this, 'unescape')),
);
}
public function unescape($value)
{
return html_entity_decode($value);
}
}
Add the following to your services.yml (or alternatively translate into xml).
将以下内容添加到 services.yml(或转换为 xml)。
services:
ha.twig.custom_extension:
class: HA\SiteBundle\Twig\CustomExtension
tags:
- { name: twig.extension }
回答by Promo IL
Or http://twig.sensiolabs.org/doc/filters/raw.html
或http://twig.sensiolabs.org/doc/filters/raw.html
{% autoescape false %}
{{ your_item }}{# your_item won't be escaped #}
{% endautoescape %}
回答by Juan De León
If you are using Drupal 8 and none of rawor autoscapeworks, this could happen because of the variable you're trying to print if it's a render array with a template holding a safe output (for example, a hl2brfilter).
如果您使用的是 Drupal 8 并且没有任何一个raw或autoscape工作,这可能是因为您尝试打印的变量是带有保存安全输出的模板(例如,hl2br过滤器)的渲染数组。
I that case, you would need to access the value through the render array and filter it, for instance:
在这种情况下,您需要通过渲染数组访问该值并对其进行过滤,例如:
{% autoescape false %}
{{ item.content['#context']['value'] }}
{% endautoescape %}

