When should some functionality be created as a filter instead of a function and vice versa?
user | function
vs
function(user)
Someone pointed out the documentation, however it does not explain when to use which. It does say this:
Functions support the same features as filters, except for the pre_escape and preserves_safety options.
What is the point of twig support function if they behave just like a filter?
Filters in Twig can be used to modify variables. Filters are separated from the variable by a pipe symbol. They may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.
As a frontend developer, you will discover the large Twig features set to help you design concise, secured and powerful templates. The training course also targets the PHP web developers who want to learn how to extend the Twig engine to create their custom Twig extensions.
Twig is a template framework and is a direct replacement for PHP template. Twig extension provides more flexibility to process almost anything inside twig. Twig extends in many ways such as tags, filters, operators, global variables, and functions.
A filter is a way to transform the displayed data.
For example if you want to display the content of a variable L1K3 Th1s
, you'll have to write a filter
(example : {{ username|l33t }}
)
A function is used when you need to compute things to render the result.
For example, the {{ dump(username) }}
function which will call internally the var_dump
php function.
Usually you write a function when you need to do heavier things than just simply transform the display of a content in a simple way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With