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: When referencing a template, the path cannot be a variable #59444

Closed
springrain opened this issue Apr 5, 2023 · 2 comments
Closed

Comments

@springrain
Copy link

springrain commented Apr 5, 2023

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

$ go version
go version go1.20.2 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

go version go1.20.2 windows/amd64
PS D:\git\gitee.com\gpress\gpress> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\47596\AppData\Local\go-build
set GOENV=C:\Users\47596\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Program Files\golang\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\Program Files\golang\gopath
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\golang\goroot
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Program Files\golang\goroot\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=D:\git\gitee.com\gpress\gpress\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\47596\AppData\Local\Temp\go-build3762823862=/tmp/go-build -gno-record-gcc-switches
 Output

What did you do?

{{ $bodystart := "/theme/default/bodystart.html" }}
{{template "/theme/default/bodystart.html" }}
package main

import (
	"bytes"
	"html/template"
	"testing"
)

func TestTemplate(t *testing.T) {
	const tpl = `
{{define "bodystart.html"}}
hello
{{end}}
{{ $bodystart := "bodystart.html" }}
{{template $bodystart}}
`

	tmpl, err := template.New("test").Parse(tpl)
	if err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	err = tmpl.Execute(&buf, nil)
	if err != nil {
		t.Fatal(err)
	}
}

What did you expect to see?

I want to use variables when referencing templates, and render normally

What did you see instead?

--- FAIL: TestTemplate (0.00s)
    temptent_test.go:20: template: test:6: unexpected "$bodystart" in template clause
FAIL
@earthboundkid
Copy link
Contributor

It’s a security thing because the compiler needs to know before evaluation if a segment is HTML or JS. You can work around it by having a FuncMap with a render function and then just manually escaping the return value.

@seankhliao
Copy link
Member

Duplicate of #33449

@seankhliao seankhliao marked this as a duplicate of #33449 Apr 5, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2023
@golang golang locked and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants