Skip to content

text/template: Clone() does not clone options like it does for funcs #43022

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

Open
bokwoon95 opened this issue Dec 5, 2020 · 1 comment
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@bokwoon95
Copy link

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

$ go version
go version go1.15.4 darwin/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bokwoon/Library/Caches/go-build"
GOENV="/Users/bokwoon/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/bokwoon/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/bokwoon/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/bokwoon/Documents/nusskylabx/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dz/0qq60fyd6z3dbr2qqy23mfpm0000gn/T/go-build334239392=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

html/template .Clone() method will copy the funcmap set with .FuncMap() over to the cloned template (as mentioned here). However .Clone() does not seem to copy the options set with .Option() over to the cloned template. Instead the cloned template reverts back to the default option missingkey=invalid. Example code demonstrating how the funcmap is copied over, but the options are not:
https://play.golang.org/p/1M00gykKOkp

package main

import (
	"html/template"
	"fmt"
	"os"
)

func main() {
	t1 := template.Must(template.
		New("t").
		Option("missingkey=error").
		Funcs(map[string]interface{}{
			"SomeFunc": func() string { return "template" },
		}).
		Parse(`{{SomeFunc}} {{.tmpl}}: {{.jdlsadjklja}}`),
	)
	t2 := template.Must(t1.Clone())
	
	if err := t1.Execute(os.Stdout, map[string]interface{}{"tmpl": "t1"}); err != nil {
		fmt.Printf("t1 error: %s\n", err)
	}
	if err := t2.Execute(os.Stdout, map[string]interface{}{"tmpl": "t2"}); err != nil {
		fmt.Printf("t2 error: %s\n", err)
	}
}

What did you expect to see?

template t1: t1 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"
template t2: t2 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"

What did you see instead?

template t1: t1 error: template: t1:1:26: executing "t" at <.jdlsadjklja>: map has no entry for key "jdlsadjklja"
template t2:

This behaviour is also observed for text/template, only that text/template outputs <no value> instead of an empty string.

Currently I am cloning a bunch of templates, and in order to preserve the options I have to store the option string somewhere in a variable and re-configure using .Option() on every newly cloned template. Copying options on .Clone() is not mentioned in the documentation, but it would be nice if it did that. Given that .Clone() already copies the funcmap, it seems reasonable for it to copy the options as well.

@toothrot toothrot changed the title html/template .Clone() does not clone options like it does for funcs html/template: Clone() does not clone options like it does for funcs Dec 8, 2020
@toothrot
Copy link
Contributor

toothrot commented Dec 8, 2020

/cc @robpike

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 8, 2020
@toothrot toothrot added this to the Backlog milestone Dec 8, 2020
@seankhliao seankhliao changed the title html/template: Clone() does not clone options like it does for funcs text/template: Clone() does not clone options like it does for funcs May 10, 2023
@joejulian joejulian mentioned this issue May 10, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

2 participants