"Begins with" in Twig template "Begins with" in Twig template php php

"Begins with" in Twig template


You can do that directly in Twig now:

{% if 'World' starts with 'F' %}{% endif %}

"Ends with" is also supported:

{% if 'Hello' ends with 'n' %}{% endif %}

Other handy keywords also exist:

Complex string comparisons:

{% if phone matches '{^[\\d\\.]+$}' %} {% endif %}

(Note: double backslashes are converted to one backslash by twig)

String contains:

{{ 'cd' in 'abcde' }}{{ 1 in [1, 2, 3] }}

See more information here: http://twig.sensiolabs.org/doc/templates.html#comparisons


Yes, Twig supports regular expressions in comparisons: http://twig.sensiolabs.org/doc/templates.html#comparisons

In your case it would be:

{% if item.ContentTypeId matches '/^0x0120.*/' %}  ...{% else %}  ...{% endif %}


You can just use the slice filter. Simply do:

{% if item.ContentTypeId[:6] == '0x0120' %}{% endif %}