twig tutorial - Filters in Twig - twig filters - twig php - twig template
Filters
- Variables can be modified using filters. To apply filter to variable follow variable name with pipe | and filter name:
{{ variable|filterName }}
For example to display variable value in uppercase use construct.
{{ variable|upper }}
Filters can be parametrized. Filter parameters are passed inside parentheses as a comma(,) separated list
{{ variable|filterName(param1, param2, ...) }}
- To round number to given precision we can use filter round, it accepts up to 2 parameters. First one specifies precision (default: 0), second one rounding method (default: common).
- To round number to 1 decimal place using common method you can use {{ number|round(1, 'common') }} or {{ number|round(1) }} as common is default method.
Filters can also be used on embedded objects & array variables:
{{ array['key'] | upper }} {{ object.text | upper }}
Filters can also be concatenated:
{% set array = "3,1,2"|split(',') %}
{{ array | sort | first }}