Skip to content
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

Allow to have named arguments in macro #885

Closed
GuillaumeGomez opened this issue Oct 21, 2023 · 5 comments · Fixed by #910
Closed

Allow to have named arguments in macro #885

GuillaumeGomez opened this issue Oct 21, 2023 · 5 comments · Fixed by #910

Comments

@GuillaumeGomez
Copy link
Contributor

It'd pretty nice to be able to have named arguments. For example:

{% macro menu_link(href, text, target) %}
...
{% endmacro menu_link %}

Then to be able to call it like this:

{% menu_link(href="/something", text="link", target="_blank") %}

It makes the code much better when reading it.

As a first step, only allowing it for macros would be nice, but potentially extending it to filters would be nice too.

@djc
Copy link
Owner

djc commented Oct 25, 2023

I worry that this would be a little foreign to users -- that is, I'm not sure it passes the design test of "it should be obvious how this gets compiled to Rust code".

@GuillaumeGomez
Copy link
Contributor Author

Does it really matter? If this is a big enough concern, we can add a new entry in the askama book explaining how it is generated.

@djc
Copy link
Owner

djc commented Oct 25, 2023

It matters to me -- I think an important design aspect of the templating language for Askama is that people can reason about how their template code gets translated into Rust code. Why don't you write the paragraph that explains this first and we can see how that looks?

@GuillaumeGomez
Copy link
Contributor Author

Sure. I'll do that (hopefully) soon.

@GuillaumeGomez
Copy link
Contributor Author

So now that #893 is merged, let's come back to this. 😆

So I had in mind to keep a Vec<String> for each macro where each element is a macro argument name. Then when we encounter a macro with named arguments (should we allw to have a mix between named and not named?), we store the expression into a variable in the same order as declared in the template (so the "computation order" remains the same) and then pass the variables in the correct order to the macro.

Does this sound like an acceptable approach?

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 a pull request may close this issue.

2 participants