简体   繁体   中英

how to use php string function in twig template engine

I want to use few php string functions in twig. For eg

How to write below code in twing?

if (!empty($message) && substr_count($message, 'blabla')) {
   ....
   ....
}

Use filters:

{% if message is not empty and ... %}
 ...
{% endif %}

I think there is not a substr_count equivalent for twig, you can either make the test and pass the result to the template or create a twig extension and implement it yourself

http://twig.sensiolabs.org/documentation

You can't, and that's the whole point of twig. Template should only take care of displaying data.

In that specific case, you could create a TWIG variable, and simply pass TRUE or FALSE, so that you twig code would look like:

{% if display_message %}
...
{% enfif %}

You can look at the list of expressions that are available in the twig template, strstr_count is not part of them :( http://twig.sensiolabs.org/doc/templates.html#expressions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM