Unescape or html decode
Asked Answered
D

4

33

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

This will output this

<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>

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.

Thanks

Domingo answered 16/3, 2013 at 19:17 Comment(0)
L
62

You can use the raw filter to make twig render raw HTML.

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

Source

Lucic answered 16/3, 2013 at 19:22 Comment(4)
Good Answer +1 However, please include an example to ensure your answer stays relevant as links can die or become missing.Shah
As always: Please be carefull when using |raw . If you raw user input, you open the gates for XSS.Mandelbaum
I absolutely love this about twig - it makes sure some coder doesn't put unescaped variable content to output, unlike other template languages where e.g. {{x}} would do that which is harder to recognize against {x} in comparison with very explicit |raw :-)Krystakrystal
Here is the updated link: twig.symfony.com/doc/3.x/filters/raw.htmlBacciferous
A
19

You should be careful with using |raw. Saying that the data is safe, means you are trusting it 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:
     ha.twig.custom_extension:
     class: HA\SiteBundle\Twig\CustomExtension
     tags:
         - { name: twig.extension }
Associationism answered 22/7, 2014 at 15:16 Comment(2)
This is elegant but in applications that are not communities or public facing web sites the 100% trust isn't really an issue :)Domingo
I agree, but it should still be a consideration, protecting the developer from themselves for example.Associationism
S
16

Or https://twig.symfony.com/doc/3.x/filters/raw.html

{% autoescape false %}
   {{ your_item }}{# your_item won't be escaped #}
{% endautoescape %}
Slide answered 11/10, 2016 at 23:58 Comment(0)
F
6

If you are using Drupal 8 and none of raw or autoscape works, 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 hl2br filter).

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 %}
Flittermouse answered 5/8, 2019 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.