purencool purencool - 3 years ago 335
Twig Question

Match sub-string is the twig

I am developing a new drupal8 website and I can't work out what I am doing wrong! All I need to do is find if a sub-string exists in the variable called output.

{% if ((output|raw matches '\b<div class="image"><\\div>\b') == true) %}
<h1>this is a test</h1>
{% endif %}

Answer Source

You could simplify the if expression removing the true comparison (is impliciti in the matches statement). So try simply this:

{% if output matches '\b<div class="image"></div>\b' %}
     <h1>this is a test</h1>
{% endif %}

Here a working example.

Hope this help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download