#664·nunjucks

Nested call/caller doesn't work.

Author: carljmCreated Feb 4, 2016Updated Oct 17, 2025
Labelsbug

Sample code:

{%- macro wrapdiv() -%}
  <div>{{ caller() }}</div>
{%- endmacro -%}

{%- macro wrapdivspan() -%}
  {%- call wrapdiv() -%}
    <span>{{ caller() }}</span>
  {%- endcall -%}
{%- endmacro -%}

{%- call wrap2divs() -%}
  Foo
{%- endcall -%}

This template should render as <div><span>Foo</span></div>, but instead it recurses infinitely and throws RangeError: Maximum call stack size exceeded.

Fortunately there's a relatively simple workaround; redefine the wrapdivspan macro as follows, to avoid nesting the call to caller() inside a call block:

{%- macro wrapdivspan() -%}
  {%- set content = caller() -%}
  {%- call wrapdiv() -%}
    <span>{{ content }}</span>
  {%- endcall -%}
{%- endmacro -%}

The nested version also fails in Jinja2 (though with No caller defined rather than infinite recursion). So it may not be a high priority to get this working in nunjucks, but it'd at least be nice if it failed with a clearer error.