golang package for multiple layouts & templates processing by single handler
- Project status: MVP is ready
- Future plans: tests & docs
This package offers 2-step template processing, where page content template called first, so it can
- change page layout (among them previous markup)
- abort processing and return error page (this will go to way 1)
- abort processing and return redirect
If page content template returns HTML, at step 2, layout template will be called for result page markup build.
According to sample, site templates tree might looks like:
tmpl
├── inc
│ ├── footer.tmpl
│ ├── head.tmpl
│ └── menu.tmpl
├── layout
│ ├── default.tmpl
│ ├── error.tmpl
│ └── wide.tmpl
└── page
├── admin
│ └── index.tmpl
├── err.tmpl
├── index.tmpl
├── page.tmpl
└── redir.tmpl
import "github.com/apisite/mulate"
func main() {
mlt := mulate.New(cfg.Template)
mlt.DisableCache(true)
allFuncs := make(template.FuncMap, 0)
err = mlt.LoadTemplates(allFuncs)
for _, uri := range mlt.Pages() {
log.Debugf("Registering uri: %s", uri)
http.HandleFunc("/"+uri, handleHTML(mlt, uri, log))
}
}
- sample
- gin-mulate - gin bindings for this package
Get http.Request data
{{ .Request.Host }}{{ .Request.URL.String }}
Get query params
{{ $param := .Request.URL.Query.Get "param" -}}
Set page title
{{ .SetTitle "admin:index" -}}
Choose layout
{{ .SetLayout "wide" -}}
Stop template processing and raise error
{{ .Raise 403 "Test error" "Error description" true }}
Stop template processing and return redirect
{{ .RedirectFound "/page" }}
in code
reqFuncs["data"] = func() interface{} {
return data
}
p, err := mlt.RenderPage(uri, reqFuncs, r)
in templates
{{range data.Todos -}}
<li>{{- .Title }}
{{end -}}
- docs, part 1
- tests, part 1
- google and ask reddit for analogs
- tests, part 2
- docs, part 2
- release
mulate means multiple layouts & templates.
The MIT License (MIT), see LICENSE.
Copyright (c) 2018 Aleksei Kovrizhkin lekovr+apisite@gmail.com