Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig filter included template

I wanted to do something like this:

{{ include("tpl.html")|f }}

But that doesn't seem to work, it just printed tpl.html without any filtering, then I tried:

{% filter f %}
    {% include "tpl.html" %}
{% endfilter %}

And it worked. I just wonder, why can't I use shorter one? Do I misunderstand something? Thanks in advance.

like image 843
Victor Fedotov Avatar asked Feb 17 '23 18:02

Victor Fedotov


2 Answers

Sorry for being that long to come back :-)

The fact is that the include function writes on the template.

If you do :

{% set s = include('FuzHomeBundle:Default:test.html.twig') %}

Which is not supposed to display something, you'll get the content of the file output anyway, and the s variable will be set to null.

If you do instead :

{% filter upper %}
{% include 'FuzHomeBundle:Default:test.html.twig' %}
{% endfilter %}

or

{% filter upper %}
{{ include('FuzHomeBundle:Default:test.html.twig' }}
{% endfilter %}

The filter tag will compile some code that control output buffer.

like image 117
Alain Tiemblo Avatar answered Feb 19 '23 09:02

Alain Tiemblo


To apply a filter on a section of code, you have to wrap it with the filter tag:

{% filter f %}
    ...
{% endfilter %}

What you were trying originally is to filter a variable which in twig is defined by the double parenthesis:

{{ variable name|filter }}

to read more check out the twig documentation on filters here

like image 39
gardni Avatar answered Feb 19 '23 09:02

gardni