mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
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
This commit is contained in:
@@ -71,6 +71,27 @@ func init() {
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.And,
|
||||
[]string{"and"},
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Or,
|
||||
[]string{"or"},
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
// getif is used internally by Hugo. Do not document.
|
||||
ns.AddMethodMapping(ctx.getIf,
|
||||
[]string{"getif"},
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Not,
|
||||
[]string{"not"},
|
||||
[][2]string{},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Conditional,
|
||||
[]string{"cond"},
|
||||
[][2]string{
|
||||
|
Reference in New Issue
Block a user