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

tpl/tplimpl: Fix template truth logic #5739

Merged
merged 1 commit into from Mar 6, 2019

Conversation

Projects
None yet
2 participants
@bep
Copy link
Member

commented Mar 6, 2019

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

@bep bep force-pushed the bep:template-if-with branch 2 times, most recently from b28c3bb to e34b693 Mar 6, 2019

@bep bep referenced this pull request Mar 6, 2019

Closed

Notes release notes 0.55 #5681

@bep bep force-pushed the bep:template-if-with branch 4 times, most recently from b8e7a56 to 28b2111 Mar 6, 2019

@bep bep requested a review from moorereason Mar 6, 2019

@bep

This comment has been minimized.

Copy link
Member Author

commented Mar 6, 2019

@moorereason I will eventually try to get this behaviour into Go, but not now ...

@bep bep force-pushed the bep:template-if-with branch from 28b2111 to f50c97a Mar 6, 2019

@moorereason

This comment has been minimized.

Copy link
Contributor

commented Mar 6, 2019

@bep, please push new commits until we're happy with it instead of force-pushing. You keep force-pushing while I'm reviewing, and I have to start over and try to figure out what you changed.

@bep

This comment has been minimized.

Copy link
Member Author

commented Mar 6, 2019

@moorereason got it ...

@moorereason
Copy link
Contributor

left a comment

I'm impressed. This is a great work-around. See other comments for requested changes.

Show resolved Hide resolved tpl/compare/truth.go Outdated
Show resolved Hide resolved tpl/tplimpl/template_ast_transformers.go Outdated
Show resolved Hide resolved tpl/tplimpl/template_ast_transformers.go Outdated
Show resolved Hide resolved tpl/compare/truth.go
@bep

This comment has been minimized.

Copy link
Member Author

commented Mar 6, 2019

@moorereason I have pushed another commit with the spelling fixes + unexported template func.

@moorereason
Copy link
Contributor

left a comment

LGTM

@bep bep force-pushed the bep:template-if-with branch from 1a5e690 to 8d97d5d Mar 6, 2019

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

@bep bep force-pushed the bep:template-if-with branch from 8d97d5d to 02eaddc Mar 6, 2019

@bep bep merged commit 02eaddc into gohugoio:master Mar 6, 2019

2 of 3 checks passed

continuous-integration/travis-ci/push The Travis CI build is in progress
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla Contributor License Agreement is signed.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.