convert_encoding not allowed in twig
Asked Answered
H

2

8

In order to convert my french html accents I try to change encoding by doing this in Twig :

{{ ddf.description | convert_encoding('UTF-8', 'HTML-ENTITIES') }}

But here is the message I get :

An exception has been thrown during the rendering of a template ("Notice: iconv(): Wrong charset, conversion from 'HTML-ENTITIES' to 'UTF-8' is not allowed").

Any idea ?

Hallett answered 20/12, 2017 at 14:13 Comment(0)
R
1

"HTML-ENTITIES" is not an encoding. It is a conversion function for special characters / accented encoding in (ISO ... UTF ...) to an equivalent in the same encoding or not depending on function and language.

Php htmlentities function: http://php.net/manual/en/function.htmlentities.php

Here is the html entities table: https://www.freeformatter.com/html-entities.html

Here is the list of encoding supported by iconv: how to get list of supported encodings by iconv library in php?

You can try using the "| raw" twig filter or create your own.

Unescape or html decode in Twig (PHP Templating)

Regimen answered 31/12, 2018 at 9:10 Comment(0)
C
0

Using automatic escaping should decode:

{% autoescape false %}
  {{ value|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% endautoescape %}

If you don't use autoescape false, it will not decode the html entities. I used this for decoding ' to '.

Source: https://twig.symfony.com/doc/2.x/tags/autoescape.html

Crozier answered 15/10, 2021 at 15:21 Comment(1)
Unfortunately this no longer works in Drupal 9.Upchurch

© 2022 - 2024 — McMap. All rights reserved.