Skip to content

feat: add caller pattern from jinja #422

Merged
GuillaumeGomez merged 19 commits intoaskama-rs:masterfrom
pollend:feature/add-support-for-caller
May 23, 2025
Merged

feat: add caller pattern from jinja #422
GuillaumeGomez merged 19 commits intoaskama-rs:masterfrom
pollend:feature/add-support-for-caller

Conversation

@pollend
Copy link
Copy Markdown
Contributor

@pollend pollend commented Apr 30, 2025

ref: #282 (comment)

I adjusted this quite heavily given the discussion in issue 282. This both adds the caller macro pattern and changes the syntax to align closer to jinja.

{% call super() %} is now {{ super() }}

https://jinja.palletsprojects.com/en/stable/templates/#call
https://jinja.palletsprojects.com/en/stable/templates/#child-template

{% macro render_dialog(title, class='dialog') -%}
    <div class="{{ class }}">
        <h2>{{ title }}</h2>
        <div class="contents">
            {{ caller() }}
        </div>
    </div>
{%- endmacro %}

{% call render_dialog('Hello World') %}
    This is a simple dialog rendered by using a macro and
    a call block.
{% endcall %}

@pollend pollend force-pushed the feature/add-support-for-caller branch 2 times, most recently from ced67ce to f0cc0cb Compare April 30, 2025 05:24
@pollend pollend marked this pull request as draft April 30, 2025 15:53
@pollend pollend marked this pull request as ready for review May 1, 2025 02:23
@pollend pollend force-pushed the feature/add-support-for-caller branch from 7368ac2 to 0c47b3f Compare May 1, 2025 02:49
@pollend pollend changed the title feat: add caller pattern for macro feat: add caller pattern from jinja May 1, 2025
Comment thread askama_derive/src/generator/node.rs Outdated
Comment thread askama_derive/src/generator/node.rs Outdated
Comment thread askama_parser/src/node.rs
Comment thread book/src/template_syntax.md Outdated
Comment thread book/src/template_syntax.md Outdated
Comment thread book/src/template_syntax.md Outdated
Copy link
Copy Markdown
Collaborator

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a very good and solid start! I think the syntax should allow to have default values etc, but can be done in a follow-up. For now though, please support more than just identifiers as arguments when calling it.

Comment thread testing/tests/calls.rs Outdated
source = r#"
{%- macro test() -%}
{{- caller("test") -}}
{{- caller("test") -}}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe replace "test" with 1 for this one? Like that we cover integer literals as well. :)

@pollend
Copy link
Copy Markdown
Contributor Author

pollend commented May 5, 2025

probably need a lot more test where to start?

@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

GuillaumeGomez commented May 5, 2025

Hard to say. ^^'

I have a few ideas:

  • What happens if you call caller() inside the call block?
  • What happens if you use named arguments in caller like caller(blue="a")?
  • Can you call a macro caller? (That should emit an error as it should not be allowed!)
  • What happens if you call caller outside of a macro?

And of course, all the same with super (which should support named arguments though).

@pollend
Copy link
Copy Markdown
Contributor Author

pollend commented May 6, 2025

the space handling was wrong didn't understand it on the first pass. I made some more adjustments hopefully the handling is correct now.

@pollend pollend force-pushed the feature/add-support-for-caller branch from 1f50bbb to a70b7bc Compare May 6, 2025 02:28
Comment on lines +1 to +14
error: missing `c` argument
--> InvalidNumberArguments.txt:3:18
"(\"a\", \"b\") -}}\n {%- endmacro -%}\n {%- call(a,b,c) test() -%}\n {{- a"...
--> tests/ui/caller_arguments.rs:5:14
|
5 | source = r#"
| ______________^
6 | | {% macro test() %}
7 | | {{- caller("a", "b") -}}
8 | | {%- endmacro -%}
... |
11 | | {%- endcall -%}
12 | | "#,
| |______^
Copy link
Copy Markdown
Contributor Author

@pollend pollend May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not look quite correct it should be the call. umm, not sure how to address this.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange indeed, seems like the span is created from the start of the arguments and not of caller.

@pollend
Copy link
Copy Markdown
Contributor Author

pollend commented May 6, 2025

having to wrap every macro in {% call .. %} and {% endcall %} seems kind of annoying. would be nice to support this syntax as well: https://jinja.palletsprojects.com/en/stable/templates/#import we can do that later

@pollend pollend force-pushed the feature/add-support-for-caller branch from f69db26 to aa1458c Compare May 6, 2025 23:58
@pollend
Copy link
Copy Markdown
Contributor Author

pollend commented May 7, 2025

I added more test cases. and addressed some problems.

@pollend pollend force-pushed the feature/add-support-for-caller branch 3 times, most recently from 34764cf to 329aa4e Compare May 7, 2025 00:52
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

having to wrap every macro in {% call .. %} and {% endcall %} seems kind of annoying. would be nice to support this syntax as well: https://jinja.palletsprojects.com/en/stable/templates/#import we can do that later

It's somewhat supported. Can be revisited though.

Comment thread book/src/template_syntax.md Outdated
Comment thread book/src/template_syntax.md Outdated
Comment thread book/src/template_syntax.md Outdated
Comment thread book/src/template_syntax.md Outdated
pollend and others added 17 commits May 22, 2025 22:50
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com>
Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com>
Signed-off-by: Michael Pollind <mpollind@gmail.com>
@GuillaumeGomez GuillaumeGomez force-pushed the feature/add-support-for-caller branch from 4b70331 to 1acbc33 Compare May 22, 2025 21:11
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

@Kijewski: The book build breakage is expected or is it a new dark magic bug? XD

@pollend
Copy link
Copy Markdown
Contributor Author

pollend commented May 22, 2025

@Kijewski: The book build breakage is expected or is it a new dark magic bug? XD

403 is a permission error

@Kijewski
Copy link
Copy Markdown
Member

I re-ran the test, and it works now. The download is retried up to 3 times, but I guess 1 second in between is a bit short.

The examples use macros, and need to be updated, too.

pollend and others added 2 commits May 22, 2025 19:35
Signed-off-by: Michael Pollind <mpollind@gmail.com>
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

CI is happy. Time to merge! Thanks a lot!

@GuillaumeGomez GuillaumeGomez merged commit 657dcea into askama-rs:master May 23, 2025
39 checks passed
@pollend pollend deleted the feature/add-support-for-caller branch May 23, 2025 13:57
@Kijewski Kijewski mentioned this pull request Jun 6, 2025
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.

3 participants