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

html/template: default missingkey behavior does not match documentation #54053

Open
rittneje opened this issue Jul 25, 2022 · 3 comments
Open
Labels
Documentation help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rittneje
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.17.12 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

https://go.dev/play/p/i3N0a5PATuI

What did you expect to see?

<b><no value></b>

This would align with the documented behavior of Template.Option.

"missingkey=default" or "missingkey=invalid"
The default behavior: Do nothing and continue execution.
If printed, the result of the index operation is the string
"".

This is what text/template outputs.

What did you see instead?

<b></b>

@seankhliao
Copy link
Member

Looks like the behavior is expected (see tests).
Though not very consistent, using {{ printf "%v" .Foo }} we can observe a value of &lt;nil&gt; or <nil>

cc @empijei

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 25, 2022
@ianlancetaylor
Copy link
Contributor

I think this is related to #18716 and #25875. When using html/template the result of .Foo is passed through the HTML escaper function. The value passed to the HTML escaper function is nil. This winds up getting printed as nothing.

I don't think we can change this behavior now. It would be nice to document it better. The current documentation says

For these internal escaping functions, if an action pipeline evaluates to a nil interface value, it is treated as though it were an empty string.

but it's not clear that a missing map key is a nil interface value.

@ianlancetaylor ianlancetaylor added this to the Backlog milestone Jul 25, 2022
@gopherbot
Copy link

Change https://go.dev/cl/419694 mentions this issue: html/template: update document about the missing map key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants