Twig: Path for embed in Twig cannot contain variables, else returns Variable not found error.

Created on 9 Dec 2018  Â·  6Comments  Â·  Source: twigphp/Twig

For some reason, setting a variable to represent a file path, and then using that to embed does not work.

This works:

{% embed 'Console::components/somecomponent' with {} only %}
{% endembed %}

But this doesn't:

{% set abc = 'Console::components/somecomponent' %}
{% embed abc with {} only %}
{% endembed %}

The above returns the following error:

Variable "abc" does not exist in "XXXXXX" at line 5.

On the other hand though, ironically, "include" seems to work as follows:

{% set abc = 'Console::components/anothercomponent' %}
{% include abc with {} only %}

If someone would be able to shed me some light I'd be very much appreciated.

Thanks!

Most helpful comment

@apfelbox embed is basically syntactic sugar for creating a new template with the template passed as parameter as the parent. It's true it's not intuitive but changing it would be a BC break.

BTW embed have a lot of strange behaviors (_self not referencing the main template, not inheriting macro imports) that are explained by how they work "under the hood", people should be aware of how they work to not be bitten by them.

All 6 comments

The embed path is executed in the context of the embed template, this means that since you used with {} only, the embed doesn't have access to the abc variable.

If you do

{% set abc = 'Console::components/somecomponent' %}
{% embed abc with { abc: abc } only %}
{% endembed %}

it would works.

@jvasseur Thanks for getting back to me so quickly. Ah, that indeed works. Sorry I didn't know about that earlier. Thank you very much for your assistance. I will close the issue.

While I understand the solution – isn't this a bug?

I mean, the variable name should be in the context of the outer template, not the embedded one. So passing the variable abc into the embed, so that it is fetched out to be used as the variable name in the calling embed just looks like a bug imo.

@stof wdyt?

@apfelbox embed is basically syntactic sugar for creating a new template with the template passed as parameter as the parent. It's true it's not intuitive but changing it would be a BC break.

BTW embed have a lot of strange behaviors (_self not referencing the main template, not inheriting macro imports) that are explained by how they work "under the hood", people should be aware of how they work to not be bitten by them.

Yeah, maybe this is an area where the underlying implementation details are shining through a bit too much. Because the example above definitely look counter-intuitive.

But as this would indeed be a BC break we probably shouldn't touch it, because it isn't a very crucial feature.

Thanks for the explanation @jvasseur

Actually I have a suggestion. Why not pass a few more examples inside the "embed" docs? That might clear things up a little bit, as I was skimming through the docs and it didn't seem to mention any of this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xxfaxy picture xxfaxy  Â·  6Comments

yguedidi picture yguedidi  Â·  4Comments

limitstudios picture limitstudios  Â·  4Comments

koflerdavid picture koflerdavid  Â·  3Comments

su-narthur picture su-narthur  Â·  6Comments