go - Variable in template's included template -
i trying put values "header" template, title , navigation links can't access variables sent main template included one.
rendering template:
... templatename := "index" args := map[string]string{ "title": "main page", "body": "this content", } pagetemplates.executetemplate(w, templatename+".html", args) ... index.html template:
{{template "header"}} <-- including "header.html" template {{.body}} <-- variable works {{template "footer"}} <-- not matter! header.html template:
{{define "header"}} <!doctype html> <html lang="en"> <head> <title>{{.title}}</title> <-- variable empty :( </head> <body> {{end}} apparently, won't work way.
maybe there's way parse/get template , put variables without putting whole header file code? send template variable main template. not seem best way it.
you can pass context template when call it. in example, changing {{template "header"}} {{template "header" .}} should sufficient.
the relevant parts official docs:
{{template "name"}}
template specified name executed nil data.{{template "name" pipeline}}
template specified name executed dot set value of pipeline.
ps: not relevant question, should remove newline between {{define "header"}} , <!doctype html>, doctype first thing in template.
Comments
Post a Comment