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.
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityI 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.
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 = '</table>' in doubleEscapedString %}
<div class="{{ hasTable ? 'hasTable' : 'hasNoTable' }}">
{{ entry.richTextFieldHandle }}
</div>