twig striptags and html special chars
Asked Answered
B

8

21

I am using twig to render a view and I am using the striptags filter to remove html tags. However, html special chars are now rendered as text as the whole element is surrounded by "". How can I either strip special chars or render them, while still using the striptags function ?

Example :

{{ organization.content|striptags(" >")|truncate(200, '...') }}

or

{{ organization.content|striptags|truncate(200, '...') }}

Output:

"QUI SOMMES NOUS ? > NOS LOCAUXNOS LOCAUXDepuis 1995,  Ce lieu chargé d’histoire et de tradition s’inscrit dans les valeurs"
Bimetallism answered 23/2, 2015 at 9:8 Comment(1)
not working but I finally solved it. thanks!Airspeed
L
41

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

Loughlin answered 23/12, 2015 at 9:1 Comment(3)
Huge thanks for that answer: short, concise and exactly what I searched.Bekki
I was still getting some strange special characters so I tried a few other things. This is what worked for me: {{ organization.content|striptags|raw }}Erechtheum
Be careful with raw, as it may have XSS issues. See also github.com/twigphp/Twig/issues/2215#issuecomment-258088927Dougald
L
8

I had a similar issue, this worked for me:

{{ variable |convert_encoding('UTF-8', 'HTML-ENTITIES') | raw }}
Latoshalatouche answered 19/7, 2017 at 12:12 Comment(0)
K
7

I was trying some of, among others, these answers:

{{ organization.content|striptags|truncate(200, true) }}
{{ organization.content|raw|striptags|truncate(200, true) }}
{{ organization.content|striptags|raw|truncate(200, true) }}
etc.

And still got strange characters in the final form. What helped me, is putting the raw filter on the end of all operations, i.e:

{{ organization.content|striptags|truncate(200, '...')|raw }}
Kalina answered 15/7, 2017 at 11:54 Comment(3)
Be careful with raw, as it may have XSS issues. See also github.com/twigphp/Twig/issues/2215#issuecomment-258088927Dougald
Yeah, but I'm using striptags first. Doesn't it ensure that it will be secure?Kalina
I added the warning for the case where some tags may still be allowed. striptags with no allowed tags would be save save but with allowed tags striptags('<allowed tags>') not. Internally it uses php function strip_tags, see also https://mcmap.net/q/630797/-prevent-xss-with-strip_tags/880188.Dougald
L
7

2022 update | tested with Drupal 8.6.16

I tried the top voted recommendation. It worked ok with some symbols but not with others.

raw filter seems to be working ok with all special characters.

like so

{{ organization.content|striptags|raw }}

Liguria answered 12/1, 2022 at 2:26 Comment(0)
B
5

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:

<?php

namespace 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';
    }
}
Bimetallism answered 23/2, 2015 at 9:14 Comment(0)
T
1

The best way to do this is :

{{ organization.content|striptags|truncate(200, '...')|raw }}

With |raw always at the end.

Don't use convert_encoding('UTF-8', 'HTML-ENTITIES'), you will encounter iconv issues.

Threat answered 18/6, 2021 at 14:58 Comment(0)
B
1

When I thought none of the above answers were working for me (convert_encoding running into iconv() issues in Drupal 9, and I thought raw, but because applying it on the argument side of an {% embed %} — as opposed to in the embedded template itself — didn't seem to help), another approach that seemed to work for me was:

{% autoescape false %}
  {{ organization.content|striptags|truncate(200, '...') }}
{% endautoescape %}

with that false part being key.

Boniface answered 21/12, 2022 at 4:42 Comment(0)
S
0

I had the same problem, I resolved it byt this function below, using strip_tags.

<?php

namespace AppBundle\Extension;

class filterHtmlExtension extends \Twig_Extension
{

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('stripHtmlTags', array($this, 'stripHtmlTags')),
        );
    }


    public function stripHtmlTags($value)
    {

        $value_displayed = strip_tags($value);


        return $value_displayed ;
    }

    public function getName()
    {
       return 'filter_html_extension';
    }
}
Sulphonamide answered 21/12, 2015 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.