-
Notifications
You must be signed in to change notification settings - Fork 27
Add filter |default
#425
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
base: master
Are you sure you want to change the base?
Add filter |default
#425
Conversation
I am not sure if Also, it would be possible to implement the trait for integers (testing if != 0), and Strings (testing if != ""). |
4af0d79
to
90c531c
Compare
Btw, no clue about a better for |
</li><li> | ||
|
||
```jinja | ||
{{ variable_or_expression | default(default_value, true) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sort of parameter without a name makes it difficult to understand a template worth reading the template docs. I'd been inclined to either:
- Replace a naked bool parameter with an enum that self documents (that may look verbose here), or
- Replace a bool with a second named method. E.g. default_if_undefined might be a good name for the false branch. Alternatively default_if_none / err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a line that {{ variable_or_expression | default(default_value, boolean = true) }}
works, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still prefer explicit over implicit a bit. This is based on an assumption that the direction of askama is a jinja-like template library with natural rust idioms rather than a jinja compatible template library that adds rust conveniences. I'm not sure whether that assumption is correct however (intuitively I'd lean towards the former as being more useful)
As a single data point. Having not seen the jinja syntax for the default
filter prior, even with the parameter named boolean
in the CoPilot suggestion rather than the bare true
, I still had no idea from reading it what that parameter meant or controlled seeing it for the first time. This suggests to me that it is poorly named in jinja, and something that we could make better pretty easily with good naming here.
Empty string seems reasonable as that's rationally returned when a string is not entered somewhere, but most of the time a zero value seems like it would be only 0 if you're using that as a flag value, which is not very rusty, so I'd perhaps implement default_if_empty for strings (and str, cow perhaps), but not default_if_zero unless there's an explicit obvious use case where it makes sense. |
I think implementing the type for integers is fine and useful. In many real-world cases you will find
|
pub fn pluralize<C, S, P>(count: C, singular: S, plural: P) -> Result<Pluralize<S, P>, C::Error> | ||
pub fn pluralize<C, S, P>(count: C, singular: S, plural: P) -> Result<Either<S, P>, C::Error> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this break semver (in a way that matters) for any consumers?
If so, would be worth keeping as Pluralize rather than version bumping the lib?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, version bumping will be needed since we plan to extend {% call %}
blocks. So it's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type was unreachable before, so its name did not matter.
fn as_filtered(&self) -> Result<Option<Self::Filtered<'_>>, Self::Error>; | ||
} | ||
|
||
const _: () = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a syntax I've seen before, what's the rationale behind it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent impacting the upper scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In here I mostly use it like a #region
. Every DefaultFilterable
is in this scope and one click hides everything.
</li><li> | ||
|
||
```jinja | ||
{{ variable_or_expression | default(default_value, true) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still prefer explicit over implicit a bit. This is based on an assumption that the direction of askama is a jinja-like template library with natural rust idioms rather than a jinja compatible template library that adds rust conveniences. I'm not sure whether that assumption is correct however (intuitively I'd lean towards the former as being more useful)
As a single data point. Having not seen the jinja syntax for the default
filter prior, even with the parameter named boolean
in the CoPilot suggestion rather than the bare true
, I still had no idea from reading it what that parameter meant or controlled seeing it for the first time. This suggests to me that it is poorly named in jinja, and something that we could make better pretty easily with good naming here.
default_value: None, | ||
}, | ||
&FilterArgument { | ||
name: "boolean", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not naming it "do_unwrap" or something along the line to better match what it's doing instead?
So, what do we want to do with the argument "boolean"? It's the name and semantics Jinja uses. The question is: How compatible do we want to be? If we don't care to be 100% compatible (and we don't), then yes, two filters would be the better option:
Fun fact minijinja does not implement |
Also, `enum Pluralize<S, P>` is renamed into `enum Either<L, R>` and exported.
Or, what about both options?
The filter |
Having two filters for the same thing sounds good to me. 👍 Just please improve the argument name. We can add an error for this specific case to tell that for better naming, What do you think @joshka? |
Supporting jinja syntax probably makes it easier to bring templates from elsewhere (and allows LLMs to fall into the pit of success by suggesting code which works even if it's sub optimal). So while I dislike the syntax, if there's a better syntax available like suggested above, then I've no complaints about also providing compatible syntax like this. I'd even go so far as to suggest that the parameter name is fine in that case (alternatively, if there's some future ability to alias it to give it an additional more descriptive name then that would work too) |
Also,
enum Pluralize<S, P>
is renamed intoenum Either<L, R>
and exported.Cc @joshka.
Resolves #424.