tmplbox

package module
v0.0.0-...-40c0909 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 6 Imported by: 1

README

go-tmplbox

Build Status

GoDoc

Yet Another (text|html)/template wrapper

WARNING

This repository has been moved to github.com/lestrrat-go/tmplbox. This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path.

SYNOPSIS

<!-- base.html -->
{{ define "root" }}
<html>
<body>
{{ block "content" }}{{ end }}
</body>
</html>
{{ end }}
<!-- index.html -->
{{ define "content" }}Hello, World!{{ end }}
// assuming you're using go-bindata's Asset function to
// retrieve template sources here.
var box := tmplbox.New(tmplbox.AssetSourceFunc(Asset))

func indexHandler(w http.ResponseWriter, r *http.Response) {
    // Assuming you have a dependency between index.html and
    // base.html (see above), GetOrCompose will automatically
    // compile and merge all of the specified templates.
    t, _ := box.GetOrCompose("index.html", "base.html")

    // Will output 
    // <!-- base.html -->
    // <html>
    // <body>
    // Hello, World!
    // </body>
    // </html>
    t.ExecuteTemplate(w, "root", nil)
}

FEATURES

  • Plays well with go-bindata
  • Easy to setup template dependency to emulate "extends", without another big library

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssetSource

type AssetSource interface {
	Get(string) ([]byte, error)
}

type AssetSourceFunc

type AssetSourceFunc func(string) ([]byte, error)

func (AssetSourceFunc) Get

func (f AssetSourceFunc) Get(s string) ([]byte, error)

type Box

type Box struct {
	// contains filtered or unexported fields
}

func New

func New(assets AssetSource) *Box

func (*Box) Compile

func (b *Box) Compile(name string) (Template, error)

Compile compiles the template specified the given name. If the template name has a ".html" suffix, html/template is used. Otherwise text/template is assumed.

func (*Box) Compose

func (b *Box) Compose(name string, deps ...string) (Template, error)

Compose creates a template instance using the templates specified in `name` and `deps`.

func (*Box) Funcs

func (b *Box) Funcs(fm FuncMap) *Box

Funcs specifies the set of template functions that will be applied to all templates that are compiled

func (*Box) Get

func (b *Box) Get(name string) (Template, error)

Get returns the template associated with asset `name`. An error is returned if the template does not exist.

func (*Box) GetOrCompose

func (b *Box) GetOrCompose(name string, deps ...string) (Template, error)

GetOrCompose is like Get, except that if the template named by `name` does not exist already, it will call Compose to generate it. Otherwise this methods turns a previously cached copy.

func (*Box) Set

func (b *Box) Set(name string, t Template) error

type FuncMap

type FuncMap map[string]interface{}

type Template

type Template interface {
	DefinedTemplates() string
	Execute(io.Writer, interface{}) error
	ExecuteTemplate(io.Writer, string, interface{}) error
	Name() string
}

Jump to

Keyboard shortcuts

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