go.rice
go.rice copied to clipboard
Nested html templates.
trafficstars
Cannot find a good example of nesting html templates with go.rice. Has anyone tried / successfully done this?
I also have this problem, have you found a solution
This is what I ended up doing ... if anyone has a better method please lmk ... https://gist.github.com/heatxsink/67e1d098f63425efd350e4625c0b13dc
I ended up doing this, I could also "walk in the folder" recursively to populate my files []string. I also shouldn't panic without sending data to the user ...
viewsBox := rice.MustFindBox("../views") //my templates are in this folder
files := []string{"bar.html",
"components/foo1.html",
"components/foo2.html",
"components/foo/bar1.html",
"components/foo/bar2.html",
"components/foo/bar2.html"}
tmpl := template.New("foo")
for _, el := range files {
fileContent, err := viewsBox.String(el)
if err != nil {
panic(err)
}
tmpl, err = tmpl.Parse(fileContent)
if err != nil {
panic(err)
}
}
I figured this much out...
func renderTemplate(w http.ResponseWriter, templateName string, data map[string]interface{}) {
tBox, err := rice.FindBox("../templates")
if err != nil {
fmt.Println(err)
}
tLayout, err := tBox.String("layout.gohtml")
if err != nil {
fmt.Println(err)
}
tPage, err := tBox.String(templateName)
if err != nil {
fmt.Println(err)
}
tmpl := template.New("")
tmpl, err = tmpl.Parse(tLayout)
if err != nil {
fmt.Println(err)
}
tmpl, err = tmpl.Parse(tPage)
if err != nil {
fmt.Println(err)
}
err = tmpl.ExecuteTemplate(w, "layout", data)
if err != nil {
fmt.Println(err)
}
}