I am currently writing some extensions in twig, but I am having trouble deciding why a certain extension should be implemented as a tag or a function.
Twig’s documentation lists the following 2 constructs and what they do:
{{ }}: used to print the result of an expression evaluation;{% %}: used to execute statements.
I am planning to implement something similiar to symfony2’s embedded controllers.
Why is it that the feature is implemented as:
{% render "AcmeDemoBundle:Demo:fancy" with { 'name': name, 'color': 'green' } %}
Since the embedded controllers function should return a fully rendered template for the requested controller, wouldn’t a function be more appropriate?
render("AcmeDemoBundle:Demo:fancy", { 'name': name, 'color': 'green' });
I was intereseted in the same question. It’s up to you to decide.
If you use
{{ }}you will be able to apply filters to the output:{{ render()|upper }}. If you use{% %}the output of your extension won’t be “sanitized” (but you can always use{{ }}witharray('is_safe' => array('all')).renderis an important construction. It’s not just a function like{{ path() }}that you can call in an expression:{{ host ~ path() }}.