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

text/template: missingkey=zero behaves different from html/template #70681

Closed
lirtistan opened this issue Dec 4, 2024 · 8 comments
Closed

text/template: missingkey=zero behaves different from html/template #70681

lirtistan opened this issue Dec 4, 2024 · 8 comments

Comments

@lirtistan
Copy link

lirtistan commented Dec 4, 2024

Go version

go version go1.23.4 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/enrico/.cache/go-build'
GOENV='/home/enrico/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE='ebony.local'
GOMODCACHE='/home/enrico/.asdf/installs/golang/1.23.4/packages/pkg/mod'
GONOPROXY='ebony.local'
GONOSUMDB='ebony.local'
GOOS='linux'
GOPATH='/home/enrico/.asdf/installs/golang/1.23.4/packages'
GOPRIVATE='ebony.local'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/enrico/.asdf/installs/golang/1.23.4/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/enrico/.asdf/installs/golang/1.23.4/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/enrico/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/enrico/Projects/golang/active/web/chi-client/go.mod'
GOWORK='/home/enrico/Projects/golang/active/web/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1859946843=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Parsing a template with help of text/template, but the missingkey=zero option behaves different from html/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...

<div class="vstack">
    <p>Foo</p>
    <no value>
</div>

with html/template i get the correct result...

<div class="vstack">
    <p>Foo</p>

</div>

also see attached screenshots...

here with text/template...
image

here with html/template...
image

What did you expect to see?

An output without , like that html example below

<div class="hstack">
    <p>Foo</p>

</div>
@ldemailly
Copy link

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?

@lirtistan
Copy link
Author

lirtistan commented Dec 4, 2024

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{})
}

lirtistan added a commit to lirtistan/template_bug that referenced this issue Dec 4, 2024
@lirtistan
Copy link
Author

here is a example of what iam doin..

https://github.com/lirtistan/template_bug

@lirtistan
Copy link
Author

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.

@lirtistan
Copy link
Author

I found the issue. See my repo for a working example (i updated the code which reflects the current state, it shows the bug)...

https://github.com/lirtistan/template_bug

@lirtistan lirtistan reopened this Dec 4, 2024
@lirtistan
Copy link
Author

looks like the same as #24963

@seankhliao
Copy link
Member

Duplicate of #24963

@seankhliao seankhliao marked this as a duplicate of #24963 Dec 4, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants