Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.36k stars 280 forks source link

Weird behavior with duplicate variable names #911

Open ctron opened 2 months ago

ctron commented 2 months ago

In a zola project I had the following:

{% set pages = get_section(path="projects/_index.md") %}
{% for page in pages.pages %}
<li><a class="link-body-emphasis" href="{{ page.path | safe }}">{{ page.extra.menuLabel | default(value=page.title) }}</a></li>
{% endfor %}

The result was was all of those menu entries had the extra.menuLabel of the current page. Not of the page from the for loop.

I quickly figured out that page was a global variable and that renaming the for loop page to something different solved the issue.

However, I found it weird that a missing page.extra.menuLabel would fall back to the global page variable. My expectation would have been that the page variable from the for loop would override the full object/struct of page. Or, would raise a conflict of a duplicate name. But not "mix" those structs.

Keats commented 2 months ago

However, I found it weird that a missing page.extra.menuLabel would fall back to the global page variable

Hu weird, it shouldn't?