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><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.
Thanks
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 %}
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With