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

Context lost in nested macro if macro was not imported. #1337

Open
lucasmoeskops opened this issue Jan 3, 2021 · 4 comments
Open

Context lost in nested macro if macro was not imported. #1337

lucasmoeskops opened this issue Jan 3, 2021 · 4 comments

Comments

@lucasmoeskops
Copy link

lucasmoeskops commented Jan 3, 2021

When I try to call a macro from inside another macro and both macros reside in the same file, the parent context seems to be lost, unless the inner macro is imported implicitly. This is not the case for imported macro's and even is solved by manually importing the current file into a new variable.

Example that doesn't work as expected:

The variables "not_shown" and "x" are not displayed in the final template, but "Only this is shown" is.

{% macro outer() %}                    
  {% set not_shown = caller()  %}  
  {% set x = 'This is not shown' %}
  {% call inner() %}                   
    {{ not_shown }}                
    {{ x }}                        
    Only this is shown             
  {% endcall %}                    
{% endmacro %}                     
                                   
{% macro inner() %}                    
  {{ caller() }}!                  
{% endmacro %}

file2.html:

{% import "path/to/other/file.html" as test %}
{% call test.outer() %}This is not shown{% endcall %}

Example that works as expected:

{% macro outer() %}
  {% import "path/to/this/file.html" as self %}
  {% set shown = caller()  %}  
  {% set x = 'This is shown' %}
  {% call self.inner() %}
    {{ shown }}                
    {{ x }}                        
    Everything is shown
  {% endcall %}                    
{% endmacro %}                     
                                   
{% macro inner() %}                    
  {{ caller() }}!                  
{% endmacro %}       

file2.html:

{% import "path/to/other/file.html" as test %}
{% call test.outer() %}This is shown{% endcall %}
@ogonkov
Copy link
Contributor

ogonkov commented Jan 4, 2021

Try to call {% import %} with context

@lucasmoeskops
Copy link
Author

Thanks for the response. Do you have an example? Because my problem is for the case where all macro's are in the same file (example 1) and I'm already not having problems when I use import (example 2).

@ogonkov
Copy link
Contributor

ogonkov commented Jan 5, 2021

Oh, you right, it seems not related to context.

@ogonkov
Copy link
Contributor

ogonkov commented Jan 5, 2021

I have inspected precompiled templates. It seems that every call create it own scope (Frame instance). But in first case, parent frame got only caller variable, while in second case it got all the variables that was set before.

Looks like a bug for me.

Looks like it is intended behaviour to preserve frame clear when calling a macro.

'frame = ' + ((keepFrame) ? 'frame.push(true);' : 'new runtime.Frame();'),

New frame created and used as frame. Previous frame value is stored in callerFrame, and returned after call is done.

I guess it would be more correct to pass caller as argument to macro.

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