duncan
duncan

Reputation: 31912

Twig: check a string doesn't end with another string

In Twig you can easily check if a string starts with or ends with another string:

http://twig.sensiolabs.org/doc/templates.html#comparisons

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

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

However, how do you find out if a string doesn't end with another one? I'm doing something like checking a filename doesn't end in .jpg for instance.

Upvotes: 13

Views: 10221

Answers (1)

duncan
duncan

Reputation: 31912

I'd tried various combinations like these unsuccesfully:

{% if not filename ends with '.jpg' %}

{% if filename ends with '.jpg' is false %}

{% if (filename ends with '.jpg') is false %}

{% if (filename ends with '.jpg') not true %}

In the end this was what I got to work:

{% if not (filename ends with '.jpg') %}

Upvotes: 35

Related Questions