I want to do something special in my template if there's an HTML table in the rich text field. I tried this:

{% if "</table>" in entry.richTextFieldHandle %}

but it doesn't work. How can I do this? Or can you think of a better way?


1 Answer 1


You can get this to work if you first escape your string using Twig escape filter. Not exactly sure what's going on, but it'll only work for me if I make Twig double escape the string like so:

{% set strategy = 'html' %}
{% set doubleEscapedString = entry.richTextFieldHandle|escape(strategy) %}
{% set hasTable = '&lt;/table&gt;' in doubleEscapedString %}

<div class="{{ hasTable ? 'hasTable' : 'hasNoTable' }}">
    {{ entry.richTextFieldHandle }}
  • Thanks. It looks quite messy though so I'm gonna look for a better way! Commented Dec 9, 2014 at 14:02

Your Answer

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

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