3

I've seen how you can remove html tags with twig in other posts here so I am using:

<p class="slider--text">{{ product.intro|striptags('<p>')|raw }}</p>

To remove the <p> tag that redactor adds automatically. However for some reason it is not removing it. Anyone have any ideas. Have I missed something glaringly obvious?

Cheers

9

Actually, the striptags filter (which is just a wrapper for the PHP strip_tags function) removes all HTML tags except the ones you pass in.

In other words, if you want to remove <p> tags, just use the striptags filter without any parameters:

{# Will remove all HTML tags #}
<p class="slider--text">{{ product.intro|striptags|raw }}</p>

If there are other tags you want to retain, you'll need to pass those in:

{# Will remove all tags except <br> and <img> #}
<p class="slider--text">{{ product.intro|striptags('<br><img>')|raw }}</p>

For a different take on stripping tags, you can use the Retcon plugin (which I wrote). With Retcon, you can remove all <p> tags (and only <p> tags; retaining their contents as well as all other tags) easily by using the retconChange filter:

<p class="slider--text">{{ product.intro|retconChange('<p>', false) }}</p>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.