4

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

1 Answer 1

12

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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