Trying to use templates in a sane way, but I think I'm holding it wrong.
Base Template is
{{define "base"}}<!DOCTYPE html>
<html lang="en">
<head>
{{template "head" .}}
{{template "meta" . }}
</head>
<body>
<!-- some html -->
{{template "body" .}}
</body>
Page Template is
{{define "head"}}<title>{{.Title}}</title>{{end}}
{{define "body"}}<h2>{{.Title}}</h2>
<p>{{.Content}}</p>
{{end}}
I can output the template like so
package main
import (
"database/sql"
"github.com/gin-gonic/gin"
"html/template"
)
func singleHandler(db *sql.DB, r *gin.Engine) gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
html := template.Must(template.ParseFiles("templates/_base.tmpl", "templates/single.tmpl", "templates/meta.tmpl"))
r.SetHTMLTemplate(html)
// get data from database
data := struct {
E *model.E
M []model.M
}{
e,
m,
}
c.Render(200, c.Engine.HTMLRender, "base", data)
})
}
But this means that if I add some template for some other block of content in the future, I'll have to go through all other files and add in the new template into template.ParseFiles(…). Also, I'd have to add in all the new code for filling in that template's data into every module that's supposed to do something else.
What's the intended way of structuring modules and nested templates?