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

Add bool to template functions #9160

Open
earthboundkid opened this issue Nov 11, 2021 · 17 comments · May be fixed by #10666
Open

Add bool to template functions #9160

earthboundkid opened this issue Nov 11, 2021 · 17 comments · May be fixed by #10666
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

#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.

@jmooring
Copy link
Member

Are we trying to do this:
https://pkg.go.dev/strconv?utm_source=gopls#ParseBool

Or "truthy" evaluation, e.g.
"something" => true

@bep
Copy link
Member

bep commented Dec 11, 2021

It needs to be truthy for it to be very useful.

@bep
Copy link
Member

bep commented Dec 11, 2021

To be clearer, in my head it should just use this: https://github.com/gohugoio/hugo/blob/master/common/hreflect/helpers.go#L63

@jmooring
Copy link
Member

Is someone going to get irritated with this?

{{ "false" | cast.ToBool }} --> true

@bep
Copy link
Member

bep commented Dec 11, 2021

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 :-)

@jmooring
Copy link
Member

LOL

@bep
Copy link
Member

bep commented Dec 11, 2021

Maybe @carlmjohnson want to chime in on this, though.

@jmooring
Copy link
Member

The use case that I keep thinking about is:

{{< shortcode foo="false" >}}
{{ if cast.ToBool (.Get "foo") }}

@bep
Copy link
Member

bep commented Dec 11, 2021

{{< shortcode foo="false" >}}

I'm pretty sure you can do

{{< shortcode foo=false >}}

(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: cast.TooBool and cast.ToTruth ... or something like that.

@jmooring
Copy link
Member

We've also trained people that "false" is false in some instances.

config.toml

buildDrafts = "false"

[params]
foo = "false"

template

{{ warnf "%T %t" .Site.BuildDrafts .Site.BuildDrafts }}  --> bool false
{{ warnf "%T %t" .Site.Params.foo .Site.Params.foo }}  --> string %!t(string=false)

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.

@bep
Copy link
Member

bep commented Dec 11, 2021

I think your examples would be better if you switched the flag:

buildDrafts = "true"

[params]
foo = "true"

They're still falsy when printed.

@earthboundkid
Copy link
Contributor Author

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.

@bep bep modified the milestones: v0.91.0, v0.92.0 Dec 20, 2021
@khayyamsaleem
Copy link
Contributor

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 "false" and "0".

@earthboundkid
Copy link
Contributor Author

earthboundkid commented Jan 7, 2022

I started looking into this, and not is built into the Go template system (not a Hugo function), but it works by calling template.IsTrue. So the natural thing to do would be to write something like

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.

@khayyamsaleem
Copy link
Contributor

So, are we looking to extend this package?

@bep
Copy link
Member

bep commented Jan 7, 2022

I have re-labeled this issue as it's not really clear what we want to do.

@bep bep modified the milestones: v0.92.0, v0.93.0 Jan 12, 2022
@bep bep modified the milestones: v0.93.0, v0.94.0 Mar 1, 2022
khayyamsaleem added a commit to khayyamsaleem/hugo that referenced this issue Jan 28, 2023
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
@khayyamsaleem khayyamsaleem linked a pull request Jan 28, 2023 that will close this issue
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
khayyamsaleem added a commit to khayyamsaleem/hugo that referenced this issue Oct 18, 2023
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
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants