Filters
Filters provide us with a way to modify variables. Filters are generally separated by a pipe character |
and may accept arguments depending on the filter's purpose. Twig currently provides us with 30-plus filters that we can apply to variables.
Let's try out filters now by applying an uppercase filter on our name
variable.
Open page.html.twig
and add the following:
{# Apply filter to name variable #} <p>{{ name|upper }} Rocks.</p>
If we save our template and refresh the browser, we will now see that the name
variable is converted to uppercase inside our paragraph tag.
We can also use filters to wrap sections of HTML and variables, which apply the filter to more than one item at a time. An example of this would be if we wanted to uppercase a whole paragraph versus just the name
variable.
Open page.html.twig
and add the following:
<p> {% filter upper %} {{ name }} is the best cms around. {% endfilter %} </p>
If we save our template and refresh the browser, we will...