tpl/tplimpl: Fix template truth logic
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 6 Mar 2019 08:07:49 +0000 (09:07 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 6 Mar 2019 21:52:38 +0000 (22:52 +0100)
commit02eaddc2fbe92c26e67d9f82dd9aabecbbf2106c
tree517220ebb06a75e89d506183a621123d49ee20c6
parentbdf47e8da80f87b7689badf48a6b8672c048d7e4
tpl/tplimpl: Fix template truth logic

Before this commit, due to a bug in Go's `text/template` package, this would print different output for typed nil interface values:

```
{{ if .AuthenticatedUser }}User is authenticated!{{ else }}{{ end }}
{{ if not .AuthenticatedUser }}{{ else }}}User is authenticated!{{ end }}
```

This commit works around this by wrapping every `if` and `with` with a custom `getif` template func with truth logic that matches `not`, `and` and `or`.

Those 3 template funcs from Go's stdlib are now pulled into Hugo's source tree and adjusted to support custom zero values, e.g. types that implement `IsZero`.

This means that you can now do:

```
{{ with .Date }}{{ . }}{{ end }}
```

And it would work as expected.

Fixes #5738
common/hreflect/helpers.go [new file with mode: 0644]
common/hreflect/helpers_test.go [new file with mode: 0644]
common/types/types.go
go.sum
tpl/compare/init.go
tpl/compare/truth.go [new file with mode: 0644]
tpl/compare/truth_test.go [new file with mode: 0644]
tpl/tplimpl/template_ast_transformers.go
tpl/tplimpl/template_ast_transformers_test.go