summaryrefslogtreecommitdiffstats
path: root/common/types
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-03-06 09:07:49 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-03-06 22:52:38 +0100
commit02eaddc2fbe92c26e67d9f82dd9aabecbbf2106c (patch)
tree517220ebb06a75e89d506183a621123d49ee20c6 /common/types
parentbdf47e8da80f87b7689badf48a6b8672c048d7e4 (diff)
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
Diffstat (limited to 'common/types')
-rw-r--r--common/types/types.go6
1 files changed, 6 insertions, 0 deletions
diff --git a/common/types/types.go b/common/types/types.go
index ca74391f8..95e72d99b 100644
--- a/common/types/types.go
+++ b/common/types/types.go
@@ -50,3 +50,9 @@ func NewKeyValuesStrings(key string, values ...string) KeyValues {
}
return KeyValues{Key: key, Values: iv}
}
+
+// Zeroer, as implemented by time.Time, will be used by the truth template
+// funcs in Hugo (if, with, not, and, or).
+type Zeroer interface {
+ IsZero() bool
+}