In PHP templates I can use php functions, for example:
foreach ($users as $user){
echo someFunction($user->getName());
}
How can I make it in TWIG?
{% for user in users %}
* {{ user.name }}
{% else %}
No user have been found.
{% endfor %}
How do I achieve this?
If you're working in symfony 2 this should also help. Concept is the same but you put the code somewhere else and format it a little differently.
http://symfony.com/doc/2.0/cookbook/templating/twig_extension.html
What you need are functions or filters. You can easily add these using the examples.
There is already a Twig extension that lets you call PHP functions form your Twig templates like:
See official extension repository.
In a twig template:
this will output "It's raining". By default, returned values are escaped in Twig.
Twig_SimpleFunction is the preferred class to use. All other function related classes in Twig are deprecated since 1.12 (to be removed in 2.0).
In a Symfony2 controller: