1

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?

Thanks.

2

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 }}
</div>
  • Thanks. It looks quite messy though so I'm gonna look for a better way! – Bruce Johnson Dec 9 '14 at 14:02

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.