purencool purencool - 1 month ago 10
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

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

Comments