-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
text/template: missingkey=zero behaves different from html/template #70681
Comments
your playground link works fine, are you saying if you run that same code in your local machine the tt output is suddenly different? it seems your screenshot is a browser “view source” so you have more code involved? maybe a typo? side note: you shouldn’t use text for html anyway? |
Hi, here is thge code that is delivering the template result to my http handler... //go:embed *.html js/* css/* views/* models/*
var assets embed.FS
func Deliver(w http.ResponseWriter, path string, data ...any) {
content, err := assets.ReadFile(path)
if err != nil {
log.Error(err)
} else {
tpl, err2 := template.New(path).Option("missingkey=zero").Parse(string(content))
if err2 != nil {
log.Error(err2)
}
var arg any
if len(data) > 0 {
arg = data[0]
}
if ok := tpl.Execute(w, arg); ok != nil {
log.Error(ok)
}
}
} here is how i use it inside my handler... type Map = map[string]string
func (route Route) Root(w http.ResponseWriter, r *http.Request) {
assets.Deliver(w, "index.html", Map{})
} |
…r more context
here is a example of what iam doin.. |
Spoiler it works as expected, so seems its my program which does wrong things, i don't understand right now. Sorry i close this issue for now and go for bug hunt. |
I found the issue. See my repo for a working example (i updated the code which reflects the current state, it shows the bug)... |
looks like the same as #24963 |
Duplicate of #24963 |
Go version
go version go1.23.4 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Parsing a template with help of
text/template
, but themissingkey=zero
option behaves different fromhtml/template
.A missing key in map[string]any is printed as
<no value>
which is different from what i expect if i render the template with.Option("missingkey=zero")
.html/template
behaves correct.See https://github.com/lirtistan/template_bug for a working example.
What did you see happen?
With
text/template
i get the following result...with
html/template
i get the correct result...also see attached screenshots...
here with

text/template
...here with

html/template
...What did you expect to see?
An output without , like that html example below
The text was updated successfully, but these errors were encountered: