Twig 中的 Unescape 或 html 解码(PHP 模板)

2022-01-22 00:00:00 php twig

我使用的是 twig 1.12.2.我的代码从代码隐藏中生成了一些元素,当使用最新版本的 twig 渲染这些元素时,它们会得到 html 编码

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

{% 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 %}

这会输出这个

<tr class="tr_even">
    <td><img src="../templates/images/sharepoint/Document.gif" border="0" alt=""/></td>
    <td>&lt;a href=&#039;?download=/ddd.png&#039;&gt;ddd.png&lt;/a&gt;</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>&lt;a href=&#039;?download=/asdasd.png&#039;&gt;asdasd.png&lt;/a&gt;</td>
    <td>2013-03-03 20:01:52</td>
    <td>66.04 KB</td>
    <td>png</td>
</tr>

当我在发送到树枝之前调试并查看数据时,它不会被转义.我还没有找到 {{ item.Link }} 的任何替代方法来按原样呈现数据.

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.

谢谢

推荐答案

你可以使用 raw 过滤器让 twig 渲染原始 html

You can use the raw filter to make twig render raw html

http://twig.sensiolabs.org/doc/filters/raw.html

{% autoescape %}
    {{ var|raw }} {# var won't be escaped #}
{% endautoescape %}

相关文章