Skip to content

feat: add caller pattern from jinja #422

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

pollend
Copy link

@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 %}

Signed-off-by: Michael Pollind <[email protected]>
@pollend pollend force-pushed the feature/add-support-for-caller branch from 505fec6 to b468ee3 Compare April 30, 2025 05:07
Signed-off-by: Michael Pollind <[email protected]>
@pollend pollend force-pushed the feature/add-support-for-caller branch 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
Copy link
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.

pollend and others added 3 commits May 5, 2025 06:55
Signed-off-by: Michael Pollind <[email protected]>
source = r#"
{%- macro test() -%}
{{- caller("test") -}}
{{- caller("test") -}}
Copy link
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. :)

Signed-off-by: Michael Pollind <[email protected]>
@pollend
Copy link
Author

pollend commented May 5, 2025

probably need a lot more test where to start?

@GuillaumeGomez
Copy link
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
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.

Signed-off-by: Michael Pollind <[email protected]>
@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
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
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.

Signed-off-by: Michael Pollind <[email protected]>
@pollend
Copy link
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
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 2 times, most recently from 053ce07 to 34764cf Compare May 7, 2025 00:50
Signed-off-by: Michael Pollind <[email protected]>
@pollend pollend force-pushed the feature/add-support-for-caller branch from 34764cf to 329aa4e Compare May 7, 2025 00:52
@GuillaumeGomez
Copy link
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.

)]
struct CallerEmpty {}
let x = CallerEmpty {};
assert_eq!(x.render().unwrap(), "nested");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add the following test to ensure backlines are not removed around "nested" please?

{%- macro test() -%}
{{- caller() -}}
{%- endmacro -%}
{%- call() test() %}
nested
{% endcall -%}

Copy link
Author

Choose a reason for hiding this comment

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

is this expected??

\nnested\n\n

Copy link
Collaborator

Choose a reason for hiding this comment

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

No it's not.

Copy link
Author

@pollend pollend May 7, 2025

Choose a reason for hiding this comment

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

umm i made a mistake somewhere, any clue what I might of done wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Likely. Good Luck with whitespace handling. 😉

If you can't figure it out, I can take a look later on.

Copy link
Author

Choose a reason for hiding this comment

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

yea ... can't quite figure it out

Signed-off-by: Michael Pollind <[email protected]>
@pollend pollend force-pushed the feature/add-support-for-caller branch from 3757bf6 to 1a87a45 Compare May 7, 2025 15:06
@GuillaumeGomez
Copy link
Collaborator

Still need to add tests I mentioned here.

@pollend
Copy link
Author

pollend commented May 7, 2025

Still need to add tests I mentioned here.

any clue what this test looks like i'm not quite following

  • Can you call a macro caller? (That should emit an error as it should not be allowed!)

@GuillaumeGomez
Copy link
Collaborator

Ah sorry, I meant a macro named caller (forgot a word).

@pollend
Copy link
Author

pollend commented May 7, 2025

  • if you have fewer arguments in the caller then the number passed it should it produce an error?
  • should we make caller/super reserved words that can't be used?
  • I don't think i deal with named arguments i could just have it immediately throw an error or should i just add it.

@GuillaumeGomez
Copy link
Collaborator

if you have fewer arguments in the caller then the number passed it should it produce an error?

Yes, number of arguments should match.

should we make caller/super reserved words that can't be used?

Yep!

I don't think i deal with named arguments i could just have it immediately throw an error or should i just add it.

Can be added in a follow-up, but then please add an issue and a test which fails on it (with a code comment in the test linking to the issue).

@pollend
Copy link
Author

pollend commented May 8, 2025

if you have fewer arguments in the caller then the number passed it should it produce an error?

Yes, number of arguments should match.

should we make caller/super reserved words that can't be used?

Yep!

I don't think i deal with named arguments i could just have it immediately throw an error or should i just add it.

Can be added in a follow-up, but then please add an issue and a test which fails on it (with a code comment in the test linking to the issue).

umm does Expr even supported named arguments?

@pollend pollend force-pushed the feature/add-support-for-caller branch from b25e322 to f70a3d8 Compare May 8, 2025 01:36
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.

2 participants