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

Support closures in templates #284

Open
agraven opened this issue Jan 5, 2020 · 10 comments · May be fixed by #611
Open

Support closures in templates #284

agraven opened this issue Jan 5, 2020 · 10 comments · May be fixed by #611

Comments

@agraven
Copy link

agraven commented Jan 5, 2020

Currently working with Options can be rather frustrating and clumsy because of the lack of support for closures inside templates. Adding support for closures would significantly improve the library's usability.

@djc
Copy link
Owner

djc commented Jan 5, 2020

Full closure support would mean arbitrary Rust code in templates, which is at odds with my design direction. What could work is figuring out how filters might interact with Option's combinator methods.

Can you give a bit more context on your use cases?

@agraven
Copy link
Author

agraven commented Jan 5, 2020

The main use case is accessing a field of a struct inside an Option, so something like session_opt.map(|session| session.user)

@djc
Copy link
Owner

djc commented Jan 5, 2020

I'm having a hard time thinking of alternative syntaxes that could cover this use case well. What might work is having highly restricted support for closures, for example by only allow single expression bodies. I'm a bit worried about creating an uncanny valley effect where it's hard to know up front what is supported and what's not, but to some extent that probably can't be prevented.

@agraven
Copy link
Author

agraven commented Jan 5, 2020

Maybe something filter-like along the lines of value|option_field(field_name)? But single-expression closures does seems like a reasonable compromise that could be incredibly useful.

@djc
Copy link
Owner

djc commented Jan 21, 2020

Okay, I'm open to having single-expression closures. However, I probably won't be able to implement them anytime soon. If anyone else is interested, I'm happy to provided guidance and/or mentoring!

@agraven
Copy link
Author

agraven commented Jan 21, 2020

I would be interested in helping implement this

@djc
Copy link
Owner

djc commented Jan 21, 2020

Cool! Okay, so probably just start with the parser, implement a new expression variant Closure(&str, Box<Expression>) and the parser for it. Then, you'll need to add code generation, along the lines of a visit_closure() method (the compiler will point out where it needs to be wired up). I think the code generation itself will be straightforward, since you can mostly rely on the existing support for the nested expression, and the closure syntax itself is also simple.

Let me know if you have questions!

@agraven
Copy link
Author

agraven commented Jan 21, 2020

Sweet, sounds pretty approachable. I'll take a stab at it.

@DJDuque
Copy link

DJDuque commented Jul 26, 2024

I think this is the issue I am having. I want to do

{% if my_vector.iter().filter(|c| condition(c)).count() > 1 %}

But this fails to compile. Is there a simple workaround? Or should I just implement this logic myself without using the closure?

@djc
Copy link
Owner

djc commented Jul 26, 2024

The simplest workaround would be to make a method on your context type that takes care of this logic, I think.

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.

3 participants