-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros in embed tags #2442
Comments
https://twig.sensiolabs.org/doc/2.x/tags/embed.html An embed is rendered in it's own contexts, so other then passing variables it doesn't inherit anything from it's parent. |
This is not true: Anyway, passing the imported macro explicitly to the embed tag like this: {% embed 'baz.html.twig' with {'bar_macros': bar_macros} %}
{% block baz %}
{{ bar_macros.bar() }}
{% endblock %}
{% endembed %} does not work either. |
This also fails with macros from the same template: {% macro bar() %}
BAR
{% endmacro %}
{% embed 'baz.html.twig' %}
{% import _self as bar_macros %}
{% block baz %}
{{ bar_macros.bar() }}
{% endblock %}
{% endembed %} but the error is different: Replacing |
@julienfalque |
Wouldn't it be more convenient that |
This solution seems to work {% embed 'baz.html.twig' with {'bar': bar_macros.bar()} %}
{% block baz %}
{{ bar|raw }}
{% endblock %}
{% endembed %} |
…ble in macros without re-importing them (fabpot) This PR was merged into the 2.x branch. Discussion ---------- Macros imported "globally" in a template are now available in macros without re-importing them closes #1790, closes #1052, closes #2414, closes #2442 Commits ------- e138164 macros imported "globally" in a template are now available in macros without re-importing them
For more complex macros or macros that it output depends on variables inside the embed block (a for loop for example) I found this solution.
|
Macros imported in a template are not accessible in
embed
tags of the same template.With following templates:
foo.html.twig
bar.html.twig
baz.html.twig
trying to render foo.html.twig will throw an exception:
Twig_Error_Runtime: Accessing Twig_Template attributes is forbidden
.The only workaround I found is to import the macros inside the
embed
tag. Explicitly passingbar_macros
usingwith
keyword does not help. I have this issue with Twig 2.3.0, is it the expected behavior?The text was updated successfully, but these errors were encountered: