Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

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

Closed
denisbertrandbe opened this issue Nov 12, 2022 · 4 comments

Comments

@denisbertrandbe
Copy link

denisbertrandbe commented Nov 12, 2022

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
Copy link
Contributor

seehat commented Nov 17, 2022

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
Copy link
Author

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
Copy link
Author

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

@seehat
Copy link
Contributor

seehat commented Nov 21, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants