<p>You can get this to work if you first escape your string using Twig <a href="http://twig.sensiolabs.org/doc/filters/escape.html" rel="nofollow"><code>escape</code></a> 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:</p>

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

&lt;div class="{{ hasTable ? 'hasTable' : 'hasNoTable' }}"&gt;
    {{ entry.richTextFieldHandle }}
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2014-12-09T14:02:22.293Z
Thanks. It looks quite messy though so I'm gonna look for a better way!
Bruce Johnson
CC BY-SA 3.0
2014-12-06T00:01:57.090Z
2014-12-06T00:11:43.813Z
2014-12-06T00:11:43.813Z
carlcs
<p>I want to do something special in my template if there's an HTML table in the rich text field. I tried this:</p>

<pre><code>{% if "&lt;/table&gt;" in entry.richTextFieldHandle %}
</code></pre>

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

<p>Thanks.</p>

<templating><richtext>
How to check for an HTML element in a rich text field?
# How to check for an HTML element in a rich text field?

(Asked by: username_0 on 2014-12-05)

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

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

**Tags:** templating, richtext


---

## 1 answers

### Answer by username_1 on 2014-12-06. Score: 2 | Accepted answer

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 %}


    {{ entry.richTextFieldHandle }}

> **Comments:**
> **username_0**: Thanks. It looks quite messy though so I'm gonna look for a better way!  

---

