Nesting filters and functions in twig | Symfony2

627 Views Asked by At

I have this simple question. Can i nesting filters in twig, using syfmony2?

Imagine i need to do the following in twig:

{{ custom_function(var)|filter1|filter2 }}

Also i need respect the order. The "filter1" filter output, has to be the "filter2" filter input.


Example:

{{ getPrice(var)|formatPrize(2,'.')|prizeSymbol('$') }} prints: $ 12.25

{{ getPrice(var)|formatPrize(3,',')|prizeSymbol('u$s') }} prints: u$s 12.251


The above requirement is useless, but i need to do something like this.

I know how to make the twig extension, but not how to solve nesting.

I apologize for my english, Google Translate did the hard work :-)

Thanks a lot.

2

There are 2 best solutions below

2
Richard On BEST ANSWER

Filters can be chained as per the documentation

E.g.

{{ name|striptags|title }}
0
BlueM On

Of course filters can be chained, as @Richard wrote. For you as the extension author, there is nothing you have to pay attention to. Your filter code gets input – regardless of whether it’s the only filter, or if (for example) it’s the 4th filter in a chain of 5 filters.