-
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: undefined variable with range after Clone #24791
Comments
Please provide a full working example to reproduce the error. It would also help if you simplified the code - I would imagine that 20 lines of Go code should be enough to showcase any template library bug. |
I am sorry, i have few line of code for reproduce this bug package main
import (
"html/template"
"os"
)
type (
Data struct {
View View
Levels []string
}
View struct{}
)
func (v View) Flash(lvl string) []interface{} {
return []interface{}{"hello"}
}
func main() {
t, err := template.New("tpl").Parse(`{{ define "layout" }}
{{ block "flash" . }}
{{ range $level := .Levels }}
{{ range $.View.Flash . }}
<div class="alert alert-{{ $level }}" role="alert">
{{ . }} <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
{{ end }}
{{ end }}
{{ end }}
{{ block "content" .}}{{end}}
{{ end }}
`)
if err != nil {
panic(err)
}
t2, err := t.Clone()
if err != nil {
panic(err)
}
_, err = t2.Parse(`{{ block "content" . }}my content{{ end }}`)
if err != nil {
panic(err)
}
err = t2.ExecuteTemplate(os.Stdout, "layout", Data{View: View{}, Levels: []string{"info", "warning", "success"}})
if err != nil {
panic(err)
}
} In one template (no clone line 41), the bug doesn't appears And the error |
Change https://golang.org/cl/106115 mentions this issue: |
Thanks for reporting this. It will be fixed in the future 1.11 release. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
commit 08304e8
What operating system and processor architecture are you using (
go env
)?What did you do?
In html/template, initialize variable and use it in inner loop
-> full code for reproduce in comment
What did you expect to see?
Have defined variables (work well with 1.9.5)
What did you see instead?
The line throw the error is the first range, not the $level in
<div class>
The text was updated successfully, but these errors were encountered: