mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 634 forks source link

Nested macro call with `caller()` misses context #1469

Open LeaVerou opened 2 days ago

LeaVerou commented 2 days ago

Sorry for the poor description, I couldn’t figure out a better one.

Testcase:

{% macro page() %}
<section class="page">
    {{ caller() | safe }}
</section>
{% endmacro %}

{% macro test_page() %}
{% set page_content = 'foobar' %}
{% call page() -%}
    {{ page_content | safe }}
{%- endcall %}
{% endmacro %}

{{ test_page() | safe }}

Should produce:

<section class="page">
    foobar
</section>

Instead produces:

<section class="page">

</section>

Jinja2 gets it right btw.

DmitrySharabin commented 2 days ago

To add to @LeaVerou's comment, if we move {% set page_content = 'foobar' %} inside the call block, it produces the expected result:

{% macro page() %}
  <section class="page">
    {{ caller() | safe }}
  </section>
{% endmacro %}

{% macro test_page() %}
  {% call page() -%}
    {% set page_content = 'foobar' %}
    {{ page_content | safe }}
  {%- endcall %}
{% endmacro %}

{{ test_page() | safe }}