template

package
v0.0.0-...-3518944 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2017 License: MPL-2.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AssetFuncName = "asset"
)

Variables

View Source
var (
	ErrNoAssetsManager       = errors.New("template does not have an assets manager")
	ErrAssetsAlreadyPrepared = errors.New("assets have been already prepared")
)

Functions

func AddFunc

func AddFunc(fn *Func)

func AddFuncs

func AddFuncs(fn []*Func)

AddFuncs registers new functions which will be available to the templates. Please, note that you must register the functions before compiling a template that uses them, otherwise the template parser will return an error.

func DefaultVFS

func DefaultVFS() vfs.VFS

DefaultVFS returns a VFS which loads templates from the tmpl directory, relative to the application binary.

func NamespacedName

func NamespacedName(ns []string, name string) string

func RegisterConverter

func RegisterConverter(ext string, c Converter)

RegisterConverter registers a template converter for the given extension. If there's already a converter for the given extension, it's overwritten by the new one.

func RegisterFunc

func RegisterFunc(name string, fn interface{}, opts ...FuncOption)

RegisterFunc registers a template func and makes it available to all templates.

Types

type CSS

type CSS template.CSS

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (CSS) ContentType

func (v CSS) ContentType() htmltemplate.ContentType

func (CSS) String

func (v CSS) String() string

type Converter

type Converter func([]byte) ([]byte, error)

Converter represents a function which converts a template source with a given extension into the source of an HTML template. Use RegisterConverter to register your own converters.

type Func

type Func struct {
	Name   string
	Fn     interface{}
	Traits FuncTrait
	// contains filtered or unexported fields
}

Func represents a function which is available to be called from a template.

func FuncPure

func FuncPure(f Func) Func

FuncPure applies the FuncTraitPure trait to the function

type FuncMap

type FuncMap map[string]*Func

type FuncOption

type FuncOption func(Func) Func

type FuncTrait

type FuncTrait int

FuncTrait indicates the traits of a template function, which allow the template engine to pass additional arguments to them.

const (
	// FuncTraitPure declares the function as pure (depends only on its arguments) and allows
	// evaluating at compile time if its arguments are constant.
	FuncTraitPure FuncTrait = 1 << iota
	// FuncTraitContext declares as a Context function. Context functions receive an additional
	// Context argument as passed to Template.ExecuteContext.
	// The Context argument type must be assignable from the type of the
	// Context used during execution. The Context argument is passed before the arguments
	// in the template function call. e.g.
	//
	//  func MyFunction(ctx *MyTemplateContext, someArg someType, someotherArg...) someThing
	//
	FuncTraitContext
	// FuncTraitState declares a State function. Note that a function might be both a Context
	// and a State function, with the State argument coming before the Context argument. The
	// State argument is a pointer to the execution state of the template, as represented by
	// the State type. State functions might use any method exported by State. e.g.
	//
	//  func MyStateFunction(s *State, someArg someType, someotherArg...) someThing
	//
	FuncTraitState
)

func (FuncTrait) HasTrait

func (ft FuncTrait) HasTrait(t FuncTrait) bool

HasTrait returns true iff ft has all traits in t

type HTML

type HTML template.HTML

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (HTML) ContentType

func (v HTML) ContentType() htmltemplate.ContentType

func (HTML) String

func (v HTML) String() string

type HTMLAttr

type HTMLAttr template.HTMLAttr

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (HTMLAttr) ContentType

func (v HTMLAttr) ContentType() htmltemplate.ContentType

func (HTMLAttr) String

func (v HTMLAttr) String() string

type JS

type JS template.JS

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (JS) ContentType

func (v JS) ContentType() htmltemplate.ContentType

func (JS) String

func (v JS) String() string

type JSStr

type JSStr template.JSStr

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (JSStr) ContentType

func (v JSStr) ContentType() htmltemplate.ContentType

func (JSStr) String

func (v JSStr) String() string

type Plugin

type Plugin struct {
	Template *Template
	Position assets.Position
}

A Plugin represents a template which can be automatically attached to another template without the parent template invoking the plugged-in template. Depending on the Position field, the plugin might be inserted at the following locations.

assets.Position.Top: Inside the <head>
assets.Position.Bottom: Just before the <body> is closed.
assets.Position.None: The plugin is added to the template, but it must invoke it explicitely.

type State

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

State represents the execution state of a template. Users should never create a State manually, but can access its methods via template state functions.

func (*State) Dot

func (s *State) Dot() reflect.Value

func (*State) Set

func (s *State) Set(name string, value interface{})

func (*State) Template

func (s *State) Template() *Template

func (*State) Unset

func (s *State) Unset(name string) bool

func (*State) Var

func (s *State) Var(name string) (reflect.Value, bool)

type Template

type Template struct {
	AssetsManager *assets.Manager
	Minify        bool

	Debug bool
	// contains filtered or unexported fields
}

func MustParse

func MustParse(fs vfs.VFS, manager *assets.Manager, name string) *Template

MustParse works like parse, but panics if there's an error

func New

func New(fs vfs.VFS, manager *assets.Manager) *Template

New returns a new template with the given VFS and assets manager. Please, refer to the documention in github.com/rainycape/vfs and gnd.la/template/assets for further information in those types. If the fs is nil, DefaultVFS() will be used.

func Parse

func Parse(fs vfs.VFS, manager *assets.Manager, name string) (*Template, error)

Parse creates a new template using the given VFS and manager and then parses the template with the given name.

func ParseFromDir

func ParseFromDir(manager *assets.Manager, dir string, name string) (*Template, error)

ParseFromDir is a conveniency function which creates a vfs.VFS rooted at dir and creates a new template from the file with the given name. It's equivalent to Parse(vfs.FS(dir), manager, name) (minus error handling).

func (*Template) AddAssets

func (t *Template) AddAssets(groups []*assets.Group) error

func (*Template) AddNamespace

func (t *Template) AddNamespace(ns string)

func (*Template) AddParseTree

func (t *Template) AddParseTree(name string, tree *parse.Tree) error

func (*Template) AddPlugin

func (t *Template) AddPlugin(plugin *Plugin) error

AddPlugin plugs another template into this one. See the Plugin type for more information.

func (*Template) Asset

func (t *Template) Asset(arg string) (string, error)

func (*Template) Assets

func (t *Template) Assets() []*assets.Group

func (*Template) Compile

func (t *Template) Compile() error

func (*Template) ContentType

func (t *Template) ContentType() string

ContentType returns the template content type, usually found by its extension.

func (*Template) Execute

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

Execute is a shorthand for ExecuteContext(w, data, nil, nil).

func (*Template) ExecuteContext

func (t *Template) ExecuteContext(w io.Writer, data interface{}, context interface{}, vars VarMap) error

func (*Template) Funcs

func (t *Template) Funcs(funcs []*Func) *Template

func (*Template) Include

func (t *Template) Include(name string) error

func (*Template) InsertTemplate

func (t *Template) InsertTemplate(tmpl *Template, name string) error

func (*Template) IsFinal

func (t *Template) IsFinal() bool

func (*Template) Name

func (t *Template) Name() string

func (*Template) Namespace

func (t *Template) Namespace() string

func (*Template) Parse

func (t *Template) Parse(name string) error

Parse parses the template starting with the given template name (and following any extends/includes directives declared in it).

func (*Template) ParseVars

func (t *Template) ParseVars(name string, vars VarMap) error

func (*Template) RawFuncs

func (t *Template) RawFuncs(funcs map[string]interface{}) *Template

func (*Template) Root

func (t *Template) Root() string

func (*Template) Trees

func (t *Template) Trees() map[string]*parse.Tree

type URL

type URL template.URL

These types are aliases for the ones defined in html/template. They will be escaped by the template engine exactly the same as their html/template counterparts.

func (URL) ContentType

func (v URL) ContentType() htmltemplate.ContentType

func (URL) String

func (v URL) String() string

type VarMap

type VarMap map[string]interface{}

Directories

Path Synopsis
sass
Package sass implements a sass compiler for assets.
Package sass implements a sass compiler for assets.
internal
htmltemplate
Package template (html/template) implements data-driven templates for generating HTML output safe against code injection.
Package template (html/template) implements data-driven templates for generating HTML output safe against code injection.
Package markdown implements a Markdown template converter.
Package markdown implements a Markdown template converter.

Jump to

Keyboard shortcuts

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