Skip to content

Support storing caller within variable #505

@seijikun

Description

@seijikun

Jinja doesn't support this pattern:

{% macro inner %}
    inner before
        {{ caller() }}
    inner after
{% endmacro %}

{% macro outer %}
    outer before
    {% call inner() %}
        {# attempt to call outer caller() in inner caller(), which should be "my_awesome_content" #}
        {{ caller() }}
    {% endcall %}
    outer after
{% endmacro %}

{% call outer() %}
   my_awesome_content
{% endcall %}

This is intentional.
However, there is a suggested workaround that looks like this:

{% macro outer %}
    outer before
    {# declare variable storing outer caller #}
    {% set caller_ = caller %}
    {% call inner() %}
        {# use outer caller from declared variable #}
        {{ caller_() }}
    {% endcall %}
    outer after
{% endmacro %}

With askama, this does currently not work, because the generated code attempts to use the field caller in the template's struct - which obviously doesn't (and probably also shouldn't ?) exist. I assume this would need special handling.

Would it be possible to support this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions