templates

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 12 Imported by: 1

README

Templates

The templates package provides a way to manage a collection of html.Template's.

Motivation

Go's html/template package is very powerful, it just needs a way to manage multiple templates, which the templates package was created to provide.

Prior to the introduction of the 'block' feature you could get away with chaining a bunch of templates together and just use Lookup to retrieve a template and then call ExecuteTemplate on said template. But if you are like me and you want to use block (lets you set up default values and does not error out if you don't define a block) You quickly find out that you cannot define a block in multiple templates. Playground Example

Basic Usage

The templates package separates the templates into two types, views and partials.

A partial is a template that will be made available to all defined views and other defined partials.

A view is a template that can use and execute against any defined partial. A view accesses a partial, a partial does not access a view

// Create a partial named base that will be used to wrap the contents of a view
templates.AddPartial("base", `
	<!DOCTYPE HTML>
	<html>
		<body>
		{{ block "header" .}}
			<header>
				I am the header that will wrap your view
			</header>
		{{ end }}

		{{ block "contents" . }}{{end}}

		{{ block "footer" . }}
			<footer>
				I am the footer that will wrap your view
			</footer>
		{{ end }}
		</body>
	</html>
`)

// Create a view named home
templates.AddView("home", `
	{{ define "contents" }}
		<p>
			Hello from the home view
		</p>
	{{ end }}
`)

// Create a view named about
templates.AddView("about", `
	{{ define "header" }}
		About page with its own idea of what it wants in the header
	{{ end }}

	{{ define "contents" }}
		Hello from the about view
	{{ end }}
`)

// Parse the templates
// NOTE: views and partiels should be created and parsed at startup.
templates.Parse()

fmt.Println("Render the home template with the base partial")
templates.MustExecute(os.Stdout, "base", "home", nil)

fmt.Println("\nRender the about template with the base partial")
templates.MustExecute(os.Stdout, "base", "about", nil)

Documentation

Overview

Example

Basic usage using the templates Default templates.

Below one partial named 'base' will be used to wrap two views, 'home' and 'about'

package main

import (
	"os"

	"github.com/biz/templates"
)

func main() {
	// Create a partial named base that will be used to wrap the contents of a view
	templates.AddPartial("base", `
<!DOCTYPE HTML>
<html>
	<body>
	{{- block "header" .}}
		<header>
			I am the header that will wrap your view
		</header>
	{{- end }}

	{{- block "contents" . }}{{- end}}

	{{- block "footer" . }}
		<footer>
			I am the footer that will wrap your view
		</footer>
	{{- end }}
	</body>
</html>`)

	// Create a view named home
	templates.AddView("home", `
{{- define "contents" }}
	<p>
		Hello from the home view
	</p>
{{- end }}
	`)

	// Create a view named about
	templates.AddView("about", `
{{- define "header" }}
	<h1>About page with its own idea of what it wants in the header</h1>
{{- end }}

{{- define "contents" }}
	Hello from the about view
{{- end }}
	`)

	// Parse the templates
	// NOTE: views and partiels should be created and parsed at startup.
	templates.Parse()

	// Render the home template wrapped by the base partial
	templates.MustExecute(os.Stdout, "base", "home", nil)

	// Render the about template wrapped by the base partial
	templates.MustExecute(os.Stdout, "base", "about", nil)
}
Output:

<!DOCTYPE HTML>
<html>
	<body>
		<header>
			I am the header that will wrap your view
		</header>
	<p>
		Hello from the home view
	</p>
		<footer>
			I am the footer that will wrap your view
		</footer>
	</body>
</html>
<!DOCTYPE HTML>
<html>
	<body>
	<h1>About page with its own idea of what it wants in the header</h1>
	Hello from the about view
		<footer>
			I am the footer that will wrap your view
		</footer>
	</body>
</html>

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = New()

Default is used to contain the default templates instance

Functions

func AddFunc

func AddFunc(name string, f interface{})

func AddFuncs

func AddFuncs(funcMap template.FuncMap)

func AddPartial

func AddPartial(name string, tmpl string)

func AddView

func AddView(name string, tmpl string)

func Delims

func Delims(left, right string)

func Execute

func Execute(w io.Writer, baseView, view string, data interface{}) error

func ExecuteSingle

func ExecuteSingle(w io.Writer, view string, data interface{}) error

func MustExecute

func MustExecute(w io.Writer, baseView, view string, data interface{})

func MustExecuteSingle

func MustExecuteSingle(w io.Writer, view string, data interface{})

func MustRender

func MustRender(baseView, view string, data interface{})

func MustRenderSingle

func MustRenderSingle(view string, data interface{})

func Parse

func Parse()

func Render

func Render(baseView, view string, data interface{}) ([]byte, error)

func RenderSingle

func RenderSingle(view string, data interface{}) ([]byte, error)

func UseExts

func UseExts(extensions []string)

Types

type Templates

type Templates struct {
	Templates  map[string]*template.Template
	Extensions map[string]bool
	// contains filtered or unexported fields
}

func New

func New() *Templates

func ParseDir

func ParseDir(dir string, stripPrefix string) (*Templates, error)

func ParseEmbed

func ParseEmbed(files embed.FS, stripPrefix string) (*Templates, error)

func (*Templates) AddFunc

func (t *Templates) AddFunc(name string, f interface{})

func (*Templates) AddFuncs

func (t *Templates) AddFuncs(funcMap template.FuncMap)

func (*Templates) AddPartial

func (t *Templates) AddPartial(name string, tmpl string)

func (*Templates) AddView

func (t *Templates) AddView(name string, tmpl string)

func (*Templates) Delims

func (t *Templates) Delims(left, right string)

func (*Templates) Execute

func (t *Templates) Execute(w io.Writer, baseView, view string, data interface{}) error

func (*Templates) ExecuteSingle

func (t *Templates) ExecuteSingle(w io.Writer, view string, data interface{}) error

func (*Templates) MustExecute

func (t *Templates) MustExecute(w io.Writer, baseView, view string, data interface{})

func (*Templates) MustExecuteSingle

func (t *Templates) MustExecuteSingle(w io.Writer, view string, data interface{})

func (*Templates) MustRender

func (t *Templates) MustRender(baseView, view string, data interface{}) []byte

func (*Templates) MustRenderSingle

func (t *Templates) MustRenderSingle(view string, data interface{}) []byte

func (*Templates) Parse

func (t *Templates) Parse()

func (*Templates) ParseDir

func (t *Templates) ParseDir(dir string, stripPrefix string) (*Templates, error)

func (*Templates) ParseEmbed

func (t *Templates) ParseEmbed(files embed.FS, stripPrefix string) (*Templates, error)

func (*Templates) Render

func (t *Templates) Render(baseView, view string, data interface{}) ([]byte, error)

func (*Templates) RenderSingle

func (t *Templates) RenderSingle(view string, data interface{}) ([]byte, error)

func (*Templates) UseExts

func (t *Templates) UseExts(extensions []string)

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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