English | Pусский
golang template engine which renders templates by executing them 2 times, one for content and another one for layout
- 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) and content type
- abort processing and return error page (this will render layout with error and without content)
- abort processing and return redirect
If page content template returns HTML, at step 2, layout template will be called for result page markup build.
- Adding template file without source recompiling
- Support plain HTML body as template (adding layout without additional markup in content)
- Attach all (pages,layouts,includes) templates at start (see lookupfs)
- Auto create routes for all page templates allowing them get required data via api (see ginapitpl)
As shown in testdata, site templates tree might looks like:
tmpl
├── includes
│ ├── inc.html
│ └── subdir1
│ └── inc.html
├── layouts
│ ├── default.html
│ └── subdir2
│ └── lay.html
└── pages
├── page.html
└── subdir3
└── page.html
All templates from the Root
directory tree are parsed in Parse()
call and program should be aborted on error.
Routes for all page URI should be set in Route()
call after that.
You can enable per request templates parsing for debugging purposes via ParseAlways(true)
but you still have to restart your program for adding or removing any template file.
- Package examples
- ginapitpl - gin bindings for this package
Get http.Request data
{{ request.Host }}{{ request.URL.String | HTML }}
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 true "Error description" }}
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 -}}
- https://stackoverflow.com/questions/42747183/how-to-render-templates-to-multiple-layouts-in-go
- https://medium.com/@leeprovoost/dealing-with-go-template-errors-at-runtime-1b429e8b854a
The MIT License (MIT), see LICENSE.
Copyright (c) 2018 Aleksei Kovrizhkin lekovr+apisite@gmail.com