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
Iterating over elements of an iterator inside a macro moves the inner collection #708
Comments
Yeah, I guess we should maybe take a reference of whatever we're passing to the macro? Would you be able to submit a PR? |
I can try to do a PR but I need some more details on how you think this should be addressed. It sounds like the problem is with the macro somehow having a different logic to decide if the iterator needs to be taken over a reference or not than the template outside the macro? Can you tell me how this works in askama and point me to the relevant code? |
By looking briefly at your loop code, it looks like askama "forgets" that {%- for foo in foos %}
{%- set list = foo.list %}
<ul>
{%- for item in list %}
<li>{{item}}</li>
{%- endfor %}
</ul>
<ol>
{%- for item in list %}
<li>{{item}}</li>
{%- endfor %}
</ol>
{%- endfor %} This produces the exact same error message. Maybe looping over a variable should always loop over that variable's reference? |
I think this is the relevant PR: 2302637. We may want to tweak that logic? Navigating whether we want to pass references or ownership is often tricky. I think it could make sense that, when referencing a |
c7697cb contains similar changes for the macro calling code, I think. |
I tried to implement your solution, but either I didn't get it quite right or just always taking a reference of macro parameters doesn't work. |
For some reason, askama won't use references of the inner collection when using macros, resulting in a compiler error
When taking the code from the macro body and placing it at the call side, it works as expected.
This is a minimal template to reproduce the problem:
Generated code by askama
I'm using askama version 0.11.1 from crates.io.
The text was updated successfully, but these errors were encountered: