Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

twig

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.

like image 888
duncan Avatar asked Jul 13 '16 16:07

duncan


People also ask

How do you split a string in twig?

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default). Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.

How do I find the URL of a twig?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

What are the features provided by twig?

Twig is a modern template engine for PHP Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. Secure: Twig has a sandbox mode to evaluate untrusted template code.


1 Answers

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') %}
like image 111
duncan Avatar answered Sep 18 '22 07:09

duncan