amteich / kirby-twig

Twig templating support for Kirby CMS
MIT License
48 stars 12 forks source link

Error when retrieving vars from included template {% include x with y %} #34

Closed denisbertrandbe closed 2 years ago

denisbertrandbe commented 2 years ago

Hello,

Vars sent to included template seems to not be sent or included :

{% include '@snippets/blocks/text.twig' with { 'foo':'bar'} %}

When I do a {{ dump(foo) }} in @snippets/blocks/text.twig, I have an error :

Twig\Error\RuntimeError
Line 3 of .../site/snippets/blocks/text.twig
Variable "foo" does not exist.

Maybe I do something wrong but it doesen't seems

Thank you

seehat commented 2 years ago

This should work, can you give me more context and post the whole text.twig file and your twig config? Are you on the latest version?

denisbertrandbe commented 2 years ago

I'm on the last version : 4.1.7. I made a plugin which define a macro to handle a kind of template suggestion for my blocks :

{% macro ui_blocks(page) %}
    {% set blueprint = page.blueprint().name %}
    {% for field in page.content().keys() %}
        {% include [
            '@snippets/blocks/' ~ blueprint ~ '/' ~ field ~ '.twig',
            '@snippets/blocks/' ~ field ~ '.twig',
            '@snippets/blocks/' ~ blueprint ~ '/default.twig',
            '@snippets/blocks/default.twig'
        ] %}
    {% endfor %}
{% endmacro %}

from my page template I call this macro :

{{- ui_blocks(page) -}}

I have a builder block (layout field) ('@snippets/blocks/builder.twig') which also use a template suggestion system :

{% set blueprint = page.blueprint().name %}
{% for layout in page.content.builder.toLayouts() %}
<section>
        {% set columns = layout.columns() %}
        {% for column in columns %}
            <div class="{{ column.span() }}">
                {% for block in column.blocks() %}
                    {% set type = block.type() %}
                    {% include [
                        '@snippets/blocks/' ~ blueprint ~ '/' ~ type ~ '.twig',
                        '@snippets/blocks/' ~ type ~ '.twig',
                        '@snippets/blocks/' ~ blueprint ~ '/default.twig',
                        '@snippets/blocks/default.twig'
                    ] with { 'foo': 'bar' } %}
                {% endfor %}
            </div>
        {% endfor %}
    </section>
{% endfor %}

So if I add a text block it calls

@snippets/blocks/text.twig

which contains

<div class="prose">
    {{ foo }}
    {{ page.content.text.kirbytext | raw }}
</div>

My twig configuration is the following :

'amteich.twig.env.functions' => [
        // Plugin db-helper
        'isHtmxRequest' => 'isHtmxRequest',
        'db' => 'db',
        'meta' => 'meta',
        // Laravel mix
        'mix' => 'mix',
    ],
    'amteich.twig.env.filters' => [],
    'amteich.twig.env.tests' => [],
denisbertrandbe commented 2 years ago

I have found the solution. Cleaning some content text files containing old references to a field solved the problem ! Thank you.

seehat commented 2 years ago

Ah good that you found the solution. Thanks for looking into it on your own. I was busy the weekend.