I have created a text/template which has the “define” block i.e “t1”. The “define” block is expecting a variable named Name. I used that “t1” in the main template. I passed the value of the variable Name, it is showing in t1 whereas it shows value in the main template
package main
import (
"log"
"os"
"text/template"
)
const tmptTxt = `
{{- define "t1" }} {{.Name}} the first tempalte {{ end -}}
{{template "t1"}}
{{ .Name }}
`
func main() {
t, err := template.New("temp").Parse(tmptTxt)
if err != nil {
log.Fatal(err)
}
t.Execute(os.Stdout, person{Name: "Prithvi"})
}
type person struct {
Name string
}
Output
<no value> the first tempalte
Prithvi
How to pass value in {{- define "t1" }} {{.Name}} the first tempalte {{ end -}}
of the code from main template?