-
Notifications
You must be signed in to change notification settings - Fork 640
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
Issues with passing variables of parent macro to child macro #912
Comments
Hm, this seems to be duplicate of #906 But since my test case is slightly different, I will leave it open for now. |
Well, turned out that scope now is so strict, that even global variables won't get into macros. This {% set setFromLayout = 'Im set from parent' %}
{% macro NavTest(argumentFromParent = setFromLayout) %}
setFromLayoutInMacro: {{ setFromLayout }}<br>
argumentFromParent: {{ argumentFromParent }}<br>
{% endmacro %}
{{ NavTest() }} will yield
Which makes macros completely unusable :( |
@ArmorDarks I'm having a hard time reproducing that. Here's what I see: var tmpl = [
"{% set setFromLayout = 'Im set from parent' %}",
"{% macro NavTest(argumentFromParent = setFromLayout) %}",
" setFromLayoutInMacro: {{ setFromLayout }}<br>",
" argumentFromParent: {{ argumentFromParent }}<br>",
"{% endmacro %}",
"{{ NavTest() }}"
].join("\n");
console.log(nunjucks.renderString(tmpl)); outputs:
|
Ah, turned out that that #912 (comment) issue appears only if you do it within {% block name %}
{% set setFromLayout = 'Im set from parent' %}
{% macro NavTest(argumentFromParent = setFromLayout) %}
setFromLayoutInMacro: {{ setFromLayout }}<br>
argumentFromParent: {{ argumentFromParent }}<br>
{% endmacro %}
{{ NavTest() }}
{% endblock %} It doesn't work same way for example from my first post in this issue, so it seems to be completely another scopes issue. |
@ArmorDarks or @fdintino any update on this issue? I ran into it today. I can confirm @ArmorDarks clarification that It does only affect the |
I am having the same issue when using nested for loop On this example. 'team.players' fails on the child block, but the funny part, it can be used on the HTML inside of that block {{ team.players }}
|
This issues seems to be related to changes of #667. Since Carl has been original author of PR and knows about Jinja2 much more than I do, I kindly ask @carljm to take a look at this issue.
As for now, in Nunjucks 3.0.0 there is no way to pass parameters of parent macro to its child macro.
Take an example:
As you can see, here I'm trying 4 ways of passing values from parrent macro (
Nav
), to it's child (Item
):argumentFromParent
)setFromParent
)argumentFromChildwithParentArgument
)argumentFromChildwithParentSet
)All those attempts will print follwoing:
In other words, all attempts to pass variable from parent to child macro will fail.
It seems to me that 3 and 4 methods should work in Jinja2, and thus in Nunjucks too. Otherwise, is it indeed impossible to share values between macros in Jinja2?
@carljm Would you have time to take a look into Jinja2 behavior closer?
The text was updated successfully, but these errors were encountered: