Help with nested template · Issue #339 · gin-gonic/gin · GitHub ここを見た。
go get github.com/gin-gonic/contrib/renders/multitemplate
してから、
import ( "github.com/gin-gonic/contrib/renders/multitemplate" )
結局。親から子を指定しないといけない? 本当は子だけで完結したいのだが・・・・。
templates := multitemplate.New() templates.AddFromFiles("contact", "templates/tra/Base.html", "templates/tra/Navbar.html", "templates/tra/Contact.html")
contact
という名前で各テンプレートを集約?
そしてcontact
をc.HTML(200, "contact", gin.H{
のとこで指定してる。
ここでshops/index.tmpl
のようにいつもしてたようなところが、contact
になるっぽい。
controller.Router.HTMLRender = templates controller.Router.GET("/contact", func(c *gin.Context) { c.HTML(200, "contact", gin.H{ "title": "Contact", "stuff": "Interesting contact stuff", }) })
templates/tra/
traってのはテキトーなので気にせず。
templates/tra/Base.html
<!DOCTYPE html> <html lang="en"> <head> <title>{{.title}}</title> </head> <body> {{ .hoge }} </body> </html>
templates/tra/Navbar.html
{{define "navbar"}} <div class="nav">This is a navbar</div> {{end}}
templates/tra/Contact.html
{{define "contact"}} <div> Contact content {{.stuff}} </div> {{end}}
しかし、親、つまり「templates/tra/Base.html」に子のtemplateの指定を書かないといけないのは不便だ。 子側から親を指定した方が便利だと思うけど、子が変わるのだがどうやるべき??