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

Nested call/caller doesn't work. #664

Open
carljm opened this issue Feb 4, 2016 · 4 comments
Open

Nested call/caller doesn't work. #664

carljm opened this issue Feb 4, 2016 · 4 comments
Labels

Comments

@carljm
Copy link
Contributor

carljm commented Feb 4, 2016

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.

@Walkeryr
Copy link

It is important when you want to nest components with macros. Is there any progress on this issue?

@carljm
Copy link
Contributor Author

carljm commented Jun 21, 2016

Nope. Usually you can easily work around it by doing a {% set caller_content = caller() %} outside the nested {% call %} and then just use that variable.

@ArmorDarks
Copy link

ArmorDarks commented Aug 14, 2016

Seems I've created duplicate of this here: #799

You can note in my issue, that actually nesting of not macros directly, but calls to them workarounds issue too:

{% call Item() %}
  {% call Item() %}
    Inner
  {% endcall %}
{% endcall %}

Though, I need to test it out exactly with your example. It's slightly different.

@demeralde
Copy link

demeralde commented Apr 11, 2018

@carljm I just tried that and it doesn't work:

{% macro service(name, index, icon, cta_text) %}
  {% set service_content = caller() %}
  {{ service_content }} {# outputting here works #}
  {% call base('section--service') %}
    {% call elements.large_title(index) %}
      <span class="text-primary">{{ name }}</span>
    {% endcall %}
    <div class="row">
      <div class="col">
        {{ service_content }} {# outputting here returns nothing #}
        <btn class="btn btn-secondary">{{ cta_text }}</btn>
      </div>
      <div class="col">
        {{ icon }}
      </div>
    </div>
  {% endcall %}
{% endmacro %}

Also note I've tried outputting service_content once instead of twice but it still doesn't work.

I'm running gulp-nunjucks ^3.1.1, which uses nunjucks ^3.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants