Skip to content
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

Nested macro functions with same name functions differently in Jinjava vs Jinja #868

Closed
jasmith-hs opened this issue May 31, 2022 · 4 comments

Comments

@jasmith-hs
Copy link
Contributor

The output of:

{% macro foo() -%}
{% macro foo() -%}
1
{%- endmacro %}
{{ foo() }} 2
{%- endmacro %}
{{ foo() }}
{{ foo() }}

Should be:

1 2

1 2

However, in Jinjava, the output is:

1 2
1

This is because macro functions defined inside of a macro function will override existing macro functions, rather than defining macro functions that exist solely within that scope.

This problem extends beyond just overriding macro functions with the same name. Macro functions defined inside of another macro function should not be usable outside of their defined scope. For example:

{% macro foo() -%}
{% macro bar() -%}
1
{%- endmacro %}
{{ bar() }} 2
{%- endmacro %}
{{ foo() }}
{{ bar() }}

The final bar() should not be calling anything, but the result in Jinjava is:

1 2
1
@hs-lsong
Copy link
Collaborator

I can't get this. why the second {{ foo() }} only outputs 1 instead of 1 2? What would be output of a third {{ foo() }} ?

@jasmith-hs
Copy link
Contributor Author

The second {{ foo() }} is not supposed to output 1, the reason it does it because the macro's definition will get overwritten while evaluating the first {{ foo() }}. It will then call the child foo() on subsequent calls, and only return 1. It will continue to return 1 for the third, fourth, etc calls because the macro function will be overwritten

@jasmith-hs
Copy link
Contributor Author

In jinja, the child macro function's definition does not change the macro functions defined on the global scope, but in jinja, then the definition can impact the global macro functions defined on outer scopes.

@hs-lsong
Copy link
Collaborator

👍

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

No branches or pull requests

2 participants