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" template.Must(template.New("test").Funcs(newTempFuncs).ParseFiles("1.temp")) not work #32269

Closed
regardfs opened this issue May 27, 2019 · 2 comments

Comments

@regardfs
Copy link

regardfs commented May 27, 2019

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

$ go version
1.12.2 darwin/adm64

Does this issue reproduce with the latest release?

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

go env Output
$ go env

What did you do?

template.Must(template.New("test").Funcs(newTempFuncs).ParseFiles("test.temp"))
will do nothing, seems that parsefiles does not suport funcs;

template.Must(template.New("test").Funcs(newTempFuncs).Parse(templatetest))
the templatetest has the some content as test.temp and works as expect...

if i change template.Must(template.New("test").Funcs(newTempFuncs).ParseFiles("test.temp")); to template.Must(template.ParseFiles("test.temp")) and remove funcs in tempfile, everythings seems fine...so I am confused...

What did you expect to see?

template.Must(template.New("test").Funcs(newTempFuncs).ParseFiles("test.temp"))
should render template

What did you see instead?

do nothing until i change to template.Must(template.ParseFiles("test.temp")) and work as expect

@regardfs
Copy link
Author

regardfs commented May 27, 2019

package main

import (
	"text/template"
	"os"
	"strings"
)

func main() {
	
	tmpl1 := template.Must(template.New("test").Funcs(template.FuncMap{"trim": strings.TrimSpace,}).ParseFiles("foo.tmpl"))
	
	tmpl1.Execute(os.Stdout, " string contains spaces both ")
}

foo.tmpl

{{.|trim}}

here i write a simple one, you could try it ...

@regardfs
Copy link
Author

.......
tmpl1 := template.Must(template.New("foo.tmpl").Funcs(template.FuncMap{"trim": strings.TrimSpace,}).ParseFiles("foo.tmpl")) worked.... new must use filename...

@golang golang locked and limited conversation to collaborators May 26, 2020
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

2 participants