Ford Enna
unread,Apr 28, 2012, 7:19:35 PM4/28/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
I trying to figure out if field substitution should work in nested
templates. Here is my sample code.
package main
import (
"html/template"
"os"
"log"
)
func main() {
article := map[string]string{
"Name":"Go Templates",
"Body":"This is a test of nested templates in Go.",
}
pageTemplate, err := template.ParseFiles("page.html", "header.html",
"footer.html")
if err != nil {
log.Fatalf("execution failed: %s", err)
}
pageTemplate.Execute(os.Stdout, article)
if err != nil {
log.Fatalf("execution failed: %s", err)
}
}
page.html
=======
<html>
{{template "header"}}
<div>
{{.Body}}
</div>
{{template "footer"}}
</html>
header.html
=========
{{define "header"}}
<div>
<h1>{{.Name}}</h1>
</div>
{{end}}
footer.html
========
{{define "footer"}}
<div>
Copyright 2012
</div>
{{end}}
The result of this code is.
<html>
<div>
<h1></h1>
</div>
<div>
This is a test of nested templates in Go.
</div>
<div>
Copyright 2012
</div>
</html>
As can be seen field substitution failed in the header between <h1></
h1>