Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter or function in TWIG?

Tags:

twig

symfony

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?

like image 478
d0001 Avatar asked Sep 18 '13 02:09

d0001


People also ask

What are filters in Twig?

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.

Is Twig front end?

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.

What is Twig extension?

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.


1 Answers

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.

like image 196
sf_tristanb Avatar answered Oct 15 '22 05:10

sf_tristanb