Skip to content

Add support for caller() in variable declaration#724

Merged
GuillaumeGomez merged 3 commits intoaskama-rs:mainfrom
GuillaumeGomez:let-caller
Apr 12, 2026
Merged

Add support for caller() in variable declaration#724
GuillaumeGomez merged 3 commits intoaskama-rs:mainfrom
GuillaumeGomez:let-caller

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Fixes #506.

I think the other big missing feature. :)

Can you double-check @seijikun please?

Also cc @Kijewski since it's big feature (although in here, I mostly moved code around and copied write_let_block code with modifications).

Comment thread askama_derive/src/generator/node.rs Dismissed
Comment thread askama_derive/src/generator/node.rs Dismissed
@seijikun
Copy link
Copy Markdown
Contributor

seijikun commented Apr 4, 2026

Awesome! This functionality is nice for macro call nesting, since this:

{%- macro outer() -%}
    {%- set caller_ = caller -%}
    <outer>
        {%- call inner() -%}
            {{- caller_() -}}
        {%- endcall -%}
    </outer>
{%- endmacro -%}

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


{%- call outer() -%}
    my content
{%- endcall -%}

can become this, which is a little easier to understand (IMHO):

{%- macro outer() -%}
    {%- set content = caller() -%}
    <outer>
        {%- call inner() -%}
            {{- content -}}
        {%- endcall -%}
    </outer>
{%- endmacro -%}

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


{%- call outer() -%}
    my content
{%- endcall -%}

Would it make sense to add a unit-test that checks that the output of these two variants is identical?
They differ, currently. I am not entirely sure why though.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Checking, might be a bug. But otherwise yes, gonna add a test to ensure both have the same output.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

GuillaumeGomez commented Apr 5, 2026

@seijikun That was a bug, but unrelated to this PR (I think?). Fixed it in the third commit.

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator Author

Merging then. :)

@GuillaumeGomez GuillaumeGomez merged commit 050be24 into askama-rs:main Apr 12, 2026
50 checks passed
@GuillaumeGomez GuillaumeGomez deleted the let-caller branch April 12, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support executing caller() method in variable declaration

3 participants