-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Feature Request: Idiomatic way to mimic Option::map #398
Comments
If the feature request is only about closures, then this is a duplicate of #284. As djc already mentioned in that issue, the problem is that adding closure support essentially requires adding support for all valid Rust syntax. If it's a reusability problem, then you could create a macro or implement it as a function in Rust. fn suffix(ver: &str) -> String {
format!("-{}", ver)
} Then within your template you can do close to what you wanted originally. {% let suffix = ver.map(self::suffix).unwrap_or_default() %}
{{ suffix }}
{# or just #}
{{ ver.map(self::suffix).unwrap_or_default() }} |
I think this is neither - I don't want above code anywhere in my template because my first impression would be what is this? and then, after understanding the code, why is that written so complicated?. Neither am I asking askama to support closures. I just want some syntax that makes it easier to deal with What you suggested seems to me more like a workaround than a fix. |
So far I've tried to avoid inventing too much syntax that isn't either (a) already in Rust or (b) already in Jinja et al and maps naturally to Rust code. If we invent syntax here, that becomes something that every user of Askama potentially has to learn, so I feel that the onus is on me to be careful about inventing things that fit in well with the rest of the design. Another option here that hasn't been mentioned is a custom filter. |
Of course, that's why I said if it was only about closures. I was just trying to shine some light on alternatives, in case you didn't know. All new ideas for dealing with |
This is very straight-forward in rust code, where we can simply write
In askama, however, this code is much longer and less readable:
It would be nice if askama had a better way to mimic
Option::map
, either supporting rust closures, or introducing a custom syntax for this.The text was updated successfully, but these errors were encountered: