twig striptags and html special chars twig striptags and html special chars symfony symfony

twig striptags and html special chars


If it could help someone else, here is my solution

{{ organization.content|striptags|convert_encoding('UTF-8', 'HTML-ENTITIES') }}

You can also add a trim filter to remove spaces before and after.And then, you truncate or slice your organization.content

EDIT November 2017

If you want to keep the "\n" break lines combined with a truncate, you can do

{{ organization.content|striptags|truncate(140, true, '...')|raw|nl2br }}


I had a similar issue, this worked for me:

{{ variable |convert_encoding('UTF-8', 'HTML-ENTITIES') | raw }}


Arf, I finally found it :

I am using a custom twig filter that just applies a php function:

<span>{{ organization.shortDescription ?: php('html_entity_decode',organization.content|striptags|truncate(200, '...')) }}</span>

Now it renders correctly

My php extension:

<?phpnamespace AppBundle\Extension;class phpExtension extends \Twig_Extension{    public function getFunctions()    {        return array(            new \Twig_SimpleFunction('php', array($this, 'getPhp')),        );    }    public function getPhp($function, $variable)    {        return $function($variable);    }    public function getName()    {        return 'php_extension';    }}