The template
library is a Go package for rendering HTML templates with advanced features like partials, layouts, and custom template functions (pipes). It is designed to work seamlessly with a flexible filesystem.
To install the library, use:
go get github.com/go-universal/template
- Efficient in-memory caching for templates.
- Flexible layout-based rendering.
- Support for global partial views.
- Robust error-safe rendering mechanism.
For layout template you can use {{ view }}
function to render child view. All global partials template can accessed by @partials/path/to/file
or template-name
.
NOTE: Use include
function to import instead of builtin template
function to prevent errors.
NOTE: In Render
, you can load multiple partial views. The first arguments must be the layout, and if the view has a partial and no layout, an empty string must be passed as the first argument.
{{ view }}
: render child template in layout. If used in non-layout template generate error!{{ exists "template name or path" }}
: check if template name or path exists.{{ include "template name or path" (optional data) }}
: includes and executes a template with the given name or path and data if exists.{{ require "template name or path" (optional data) }}
: includes and executes a template with the given name or path and data or returning an error if the template does not exist.
<!-- parts/header.tpl -->
{{ define "site-header" }}
<header>...</header>
{{ end }}
<!-- parts/sub/footer.tpl -->
<footer>...</footer>
<!-- pages/home.tpl -->
<section>
<h1>Home Page</h1>
<p>{{ .Title }}</p>
</section>
{{ define "title" }}Home Page{{ end }}
<!-- layout.tpl -->
<html>
<head>
{{ if exists "title" }}
<title>{{ include "title" }}</title>
{{ else }}
<title>My App</title>
{{ end }}
</head>
<body>
{{- require "site-header" . }}
{{- view }}
{{- include "@partials/sub/footer" }}
</body>
</html>
package main
import (
"os"
"github.com/go-universal/fs"
"github.com/go-universal/template"
)
func main() {
source := fs.NewDir("./views")
tpl := template.New(source, template.WithPartials("parts"))
err := tpl.Load()
if err != nil {
panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := template.Ctx().Add("Title", "Hello, World!")
err = tpl.Render(w, "pages/home", data, "layout")
if err != nil {
// Render error page with no layout and two partial views
tpl.Render(w, "pages/errors", nil, "", "pages/err-partial/500", "pages/err-partial/contact")
}
})
}
tpl := template.New(fs,
template.WithRoot("."),
template.WithPartials("partials"),
template.WithExtension(".tpl"),
template.WithDelimeters("{{", "}}"),
template.WithEnv(true),
template.WithCache(),
template.WithUUIDPipe(),
template.WithTernaryPipe(),
)
type Template interface {
Load() error
Render(w io.Writer, view string, data interface{}, layouts ...string) error
Compile(name, layout string, data any) ([]byte, error)
}
WithRoot(root string) Options
: Sets the root directory for templates.WithPartials(path string) Options
: Sets the directory for partial templates.WithExtension(ext string) Options
: Sets the file extension for templates.WithDelimeters(left, right string) Options
: Sets the delimiters for template tags.WithEnv(isDev bool) Options
: Sets the environment mode (development or production).WithCache() Options
: Enables template caching.WithPipes(name string, fn any) Options
: Registers a custom function (pipe) for templates.
Helper struct to pass data to template.
type Context struct {
data map[string]any
}
func Ctx() *Context
func ToCtx(v any) *Context
func (ctx *Context) Add(k string, v any) *Context
func (ctx *Context) Map() map[string]any
WithUUIDPipe() Options
: Adds a UUID generation pipe.WithTernaryPipe() Options
: Adds a ternary operation pipe.WithNumberFmtPipe() Options
: Adds a number formatting pipe.WithRegexpFmtPipe() Options
: Adds a regular expression formatting pipe.WithJSONPipe() Options
: Adds a JSON formatting pipe.WithDictPipe() Options
: Adds a dictionary creation pipe.WithIsSetPipe() Options
: Adds a pipe to check if a value is set.WithAlterPipe() Options
: Adds a pipe to alter a value.WithDeepAlterPipe() Options
: Adds a pipe to deeply alter a value.WithBrPipe() Options
: Adds a pipe to convert\n
to<br>
.
This library is licensed under the ISC License. See the LICENSE file for details.