Tags vs functions in twig extensions

I am currently writing several extensions in twig, but it is difficult for me to decide why some extension should be implemented as a tag or function.

The twig documentation contains the following 2 constructs and what they do:

  • {{ }} : used to print the result of evaluating an expression;
  • {% %} : Used to execute statements.

I plan to implement something similar to symfony2's built-in controllers .

Why the function is implemented as:

 {% render "AcmeDemoBundle:Demo:fancy" with { 'name': name, 'color': 'green' } %} 

Since the function of the built-in controllers should return a fully visualized template for the requested controller, would the function be more suitable?

 render("AcmeDemoBundle:Demo:fancy", { 'name': name, 'color': 'green' }); 
+4
source share
1 answer

I was interested in the same question. It is up to you.

If you use {{ }} , you can apply filters to the output: {{ render()|upper }} . If you use {% %} array('is_safe' => array('all') {% %} , the output of your extension will not be "sanitized" (but you can always use {{ }} with array('is_safe' => array('all') ).

render is an important construct. This is not just a function like {{ path() }} that you can call in the expression: {{ host ~ path() }} .

+2
source

Source: https://habr.com/ru/post/1391194/


All Articles