-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Add bool to template functions #9160
Comments
Are we trying to do this: Or "truthy" evaluation, e.g. |
It needs to be |
To be clearer, in my head it should just use this: https://github.com/gohugoio/hugo/blob/master/common/hreflect/helpers.go#L63 |
Is someone going to get irritated with this?
|
Someone is going to be irritated by whatever we do. I would be very irritated if it was the other way around -- pick your enemy :-) |
LOL |
Maybe @carlmjohnson want to chime in on this, though. |
The use case that I keep thinking about is:
|
I'm pretty sure you can do
(the shortcode parser supports booleans and numbers) I do see your point, though, for data coming in from misc. data sources, but I suspect there is a bad gotcha somewhere ... I suspect it would be better to create two functions with different semantics: |
We've also trained people that "false" is false in some instances. config.toml
template
And I've seen a lot of foo = "false" in config files. I'm not advocating anything, just thinking of other cases where the function (or functions) might be used. |
I think your examples would be better if you switched the flag:
They're still falsy when printed. |
My expectation is that all strings are true except for the empty string. A separate helper that understands “false” and “0” as false would probably be okay, but I try to avoid that whenever I can. |
Hi! If @carlmjohnson 's last comment is acceptable behavior, I'd like to be able to take up this issue as my first contribution! But if there's more discussion going on, I would agree that universally familiar truthyness behavior would be best, and leave it up to the user to handle cases like |
I started looking into this, and func Bool(v interface{}) bool {
b, _ := template.IsTrue(v)
return b
} The tricky part, which I didn't have time to figure out, is hooking it up to Hugo's system of template function namespaces and then writing some tests and documentation. |
So, are we looking to extend this package? |
I have re-labeled this issue as it's not really clear what we want to do. |
The behavior of `truth` and `bool` is described in the corresponding test cases and examples. The decision-making around the behavior is a based on combination of the existing behavior of strconv.ParseBool in go and the MDN definition of "truthy" as JavaScript has the most interop with the Hugo ecosystem. Addresses gohugoio#9160 and (indirectly) gohugoio#5792
#5792 proposes improving
cond
to accept non-bool first arguments. That should also be done, but it would also be good to have a freestanding function to convert values to bools.As a workaround, now one can do
{{ $x := $y | not | not }}
, but that's a bit ugly.The text was updated successfully, but these errors were encountered: