Twig filters allow you to make changes to markup right in the template. Let's use our node title to test some string filters.
-
First, we want to get the node title as a string.
-
Try
{{ kint(node) }}
in your theme's node.html.twig file. UnderAvailable methods
observelabel()
. It is a public function so we can use it in our twig template. -
Near your other variable declaration in your theme's node.html.twig add:
{% set title_string = node.label %}
-
Test it with the following filters.
clean_class: {{ title_string|clean_class }} <br/> upper: {{ title_string|upper }} <br/> length: {{ title_string|length }} <br/>
-
If you have time, try creating arrays, loops and object to test some of the other amazing filters for twig See: https://twig.symfony.com/doc/2.x/filters/index.html.
- When should one use Twig filters instead of CSS or PHP?
¡No Pare! !Sigue! !Sigue! Exercise 11 - Twig Blocks