package module
v0.1.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2023 License: BSD-2-Clause Imports: 13 Imported by: 0



Package tmpl handles loading and rendering HTML templates.

import (


The package may be used under the terms of the BSD 2-Clause License a copy of which may be found in the LICENSE file.

Unless you explicitly state otherwise, any contribution submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.



Package tmpl handles loading and rendering HTML templates.

Primarily this package wraps the "html/template" package but adds support for base templates, internationalization, and live-reloading in dev-mode.

Base Templates

To use base templates first define them separately from your standard templates. In each one define sections to override, for example, a base template that constructs a simple page might look like this:

{{ define "base" }}
<!doctype HTML>
	<h1>{{ define "title" }}</h1>
{{ end }}

Then in your regular tempaltes you can re-define "title" and call base:

{{ template "base" . }}
{{ block "title" }}My Page{{ end }}



This section is empty.


This section is empty.


func SetFlash

func SetFlash(w http.ResponseWriter, flash Flash)

SetFlash sets a flash message using a cookie. Flash messages can also be set when rendering the response, but since this will not work for redirects or methods without a response, sometimes we need to set a cookie and read the value from there.


type Flash

type Flash struct {
	Message string
	Type    FlashType

Flash is a flash message which may be used to convey information to the user.

type FlashType added in v0.0.2

type FlashType string

FlashType is the type of a flash message.

const (
	FlashDanger  FlashType = "danger"
	FlashSuccess FlashType = "success"
	FlashWarn    FlashType = "warn"

A list of flash types.

type Option added in v0.1.0

type Option func(*Template)

Option is used to configure a template.

func BaseFS added in v0.1.0

func BaseFS(vfs fs.FS) Option

BaseFS is an option that loads all files in the given filesystem as base layout templates that are available for other templates to call.

func Catalog added in v0.1.0

func Catalog(c catalog.Catalog) Option

Catalog gives the template access to the provided catalog of translations.

func Dev added in v0.1.0

func Dev(dev bool) Option

Dev returns an option that enables live-reloading of templates.

This is most often used in conjunction with a build directive that embeds the templates in production mode, or uses the local filesystem in dev mode.

func FS added in v0.1.0

func FS(vfs fs.FS) Option

FS is an option that loads all files in the given filesystem as individual templates.

func Funcs added in v0.1.0

func Funcs(f template.FuncMap) Option

Funcs adds the elements of the argument map to the templates function map.

type Page

type Page struct {
	Path    string
	URL     *url.URL
	Domain  string
	Host    string
	XSRF    string
	Lang    language.Tag
	Printer *message.Printer
	Flash   Flash
	UID     int

	// Data may be set by a template renderer when the template is executed
	// and should not be set by callers of this package (except by setting the
	// extraData parameters on a template renderer).
	// It will contain data that can only be known at render time and not when the
	// renderer is constructed (which may or may not be the same).
	Data interface{}

Page represents data that can apply generally to any page.

func (Page) T

func (p Page) T(key message.Reference, a ...interface{}) string

T attempts to translate the string "s" using p.Printer.

type RenderFunc

type RenderFunc func(uid int, flash Flash, w http.ResponseWriter, r *http.Request, extraData interface{}) error

RenderFunc is the type used to render templates into pages. For more information see Renderer.

func Renderer

func Renderer(domain, xsrfKey, tmplName string, tmpls Template, data func(Page) interface{}) RenderFunc

Renderer returns a function that can be used to render pages using the provided templates.

The data function is used to construct the data passed to the template (which should embed the provided Page). If it is nil, the page is used. If xsrfKey is provided, an XSRF token is constructed and passed to the page. If a flash message is passed to the returned function, it is displayed immediately and overrides any flash message set in a cookie (without clearing the cookie). To set a flash message in a cookie (eg. to persist it across a redirect) see SetFlash.

type Template

type Template struct {
	// contains filtered or unexported fields

Template wraps an "html/template".Template and adds better support for base templates, internationalization, and live reloading functionality for easy development.

func New

func New(opt ...Option) (Template, error)

New creates a new undefined template with the given options.

func (Template) Execute

func (t Template) Execute(wr io.Writer, data interface{}) error

Execute executes the template, reloading it first if we're in dev mode.

func (Template) ExecuteTemplate

func (t Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

ExecuteTemplate executes the named template, reloading it if we're in dev mode.

func (Template) Templates added in v0.1.0

func (t Template) Templates() []*template.Template

Templates returns a slice of all templates in the list.

If we're in dev-mode and an error is encountered while reloading it is silently ignored and a nil slice is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL