Add format filter that swaps the first two arguments. - #345
Conversation
|
I don't see this adding much value -- it seems more likely to be confusing. Sorry! |
|
If you don't want to add this, that's fine, but I hope you can at least take a moment to consider how this API is significantly more ergonomic. For one thing, it better matches the mental model of other filters: data flows in from the left, configuration is passed as a param (see, for instance, |
|
Should have led with that! I hadn't realized the composition advantage. How do you feel about restricting it to a single argument (so no more arguments after the format string)? |
|
Limiting it to one argument makes sense, it would get awfully confusing with more than one. For that matter, I'm not sure there's much point to putting any static text in the format string in the context of a templating language. Would it be crazy to automatically wrap the format in curly braces?
On the other hand, perhaps there would be a reason to include static text, to support composition with downstream filters:
Though that suggests a use-case for additional parameters (most useful if named) :
Although at that point you're probably better off writing a custom filter. |
|
One of the core design choices in Askama is to make the templating language Rust-like. Eliding the braces seems antithetical to that. Let's start with the simple two-argument version in this PR and we can discuss extending it once we run into a compelling use case. |
|
Updated. What do you think about the name? I'd prefer to be explicit about the difference. Perhaps As an aside, have you considered using |
djc
left a comment
There was a problem hiding this comment.
Some minor nits. Please also squash all the commits together into a single one.
I think the name is probably okay. Compound names with _ seem a bit ugly -- I'm open to any non-compound alternatives you come up with? Might be nice to use the documentation to point from one to the other and vice versa, though. And ideally, also update the book sources.
c680198 to
3baad58
Compare
This allows a more natural filter usage: `{{ val | fmt("{:?}") }}`
as well as enabling convenient filter composition:
`{{ price | to_f64 | fmt("${:.2}") | center }}`
3baad58 to
75f2a23
Compare
|
Updated per all feedback and squashed the commits. I'm happy with the name; given docs examples and links both ways it seems pretty discoverable. Let me know if you've got any other thoughts. Thanks! |
|
Thanks for your contribution, much appreciated (especially for sticking with it despite the initial negative feedback)! |
|
Totally understand, I should have provided more context from the start. Thanks! |
Swapping the first two arguments allows a more natural filter usage:
{{ val | fmt("{:?}") }}.Happy to bikeshed the name.