question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using {{ caller() }} within {% call %} fails

See original GitHub issue

The actual template where I encountered the issue first was more boilerplate-ish, but I this one describes the problem in a more intuitive way.

Suppose we have the following template:

{%- macro _tag(tag, id=none, classes=(), attrs={}) -%}
  <{{ tag }}
      {% if id %} id="{{ id }}" {% endif %}
      {% if classes %} class="{{ classes|join(' ') }}" {% endif %}
      {{ attrs|xmlattr }}>
    {% if caller %} {{ caller() }} {% endif %}
  </{{ tag }}>
{%- endmacro -%}

along with a higher level template that tries to {% call %} it for some specific case:

{%- macro _div() -%}
  {% call _tag('div', *varargs, **kwargs) %}
    {% autoescape false %}
      {% if caller %} {{ caller() }} {% endif %}
    {% endautoescape %}
  {% endcall %}
{%- endmacro -%}

Intuitively, doing {% call _div(...) %} <b>foo</b> {% endcall %} should pass <b>foo</b> to the _tag macro, which would then render it (verbatim, thanks to autoescape). But the actual result is UndefinedError: No caller defined on the {{ caller() }} line inside _div. It would seem that the scope of caller does not extend into the {% call %} block, thereby preventing chained calls such as the one depicted above.

There is an ugly workaround that uses the new block assignment feature from 2.8:

{%- macro _div() -%}
  {% set content %}
    {% if caller %} {{ caller() }} {% endif %}
  {% endset %}
  {% call _tag('div', *varargs, **kwargs) %}
    {% autoescape false %} {{ content }} {% endautoescape %}
  {% endcall %}
{%- endmacro -%}

but, of course, the original version would be much more preferable.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
benallardcommented, Oct 6, 2014

Small note about the workaround:

The new feature of 2.8 is not needed as this is also working fine:

{% macro a() %}
  start of a
  {% set content=caller() %}
  {% call b() %}
    {{ content }}
  {% endcall %}
  end of a
{% endmacro %}

{% macro b() %}
  start of b
  {{ caller() }}
  end of b
{% endmacro %}

{% call b() %}
 inside b only
{% endcall %}

{% call a() %}
  inside a
{% endcall %}
1reaction
mitsuhikocommented, Jan 7, 2017

Yeah, we might want to put that into the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot call failures from an Amazon Connect instance
Confirm that the number on the instance is attached to a contact flow by doing the following: In the Amazon Connect console, choose...
Read more >
Top 8 Ways to Fix Call Failed Issues on iPhone - Guiding Tech
Do you get a Call Failed error when you make calls or in the middle of an ongoing conversation? Check out eight ways...
Read more >
Failed to write into Excel - call was rejected by callee
This article describes how to resolve Failed to write into Excel - call was rejected by callee error.
Read more >
Troubleshooting Incoming Calls on a Twilio Phone Number
In this state, incoming calls are not tracked in your Twilio project, and attempted callers will likely hear a standard error message (Example:...
Read more >
8 ways to fix a call failed message on your iPhone
Having calls fail on your iPhone is annoying, but you can fix it in several ways. Here's what to do.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found