template

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2017 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromJSON

func FromJSON(o interface{}) (interface{}, error)

FromJSON decode the input JSON encoded as string or byte slice into a map.

func FromMap

func FromMap(m map[string]interface{}, raw interface{}) error

FromMap decodes map into raw struct

func Index added in v0.3.0

func Index(srch interface{}, array interface{}, strictOptional ...bool) int

Index returns the index of search in array. -1 if not found or array is not iterable. An optional true will turn on strict type check while by default string representations are used to compare values.

func QueryObject

func QueryObject(exp string, target interface{}) (interface{}, error)

QueryObject applies a JMESPath query specified by the expression, against the target object.

func SplitLines

func SplitLines(o interface{}) ([]string, error)

SplitLines splits the input into a string slice.

func ToJSON

func ToJSON(o interface{}) (string, error)

ToJSON encodes the input struct into a JSON string.

func ToMap

func ToMap(raw interface{}) (map[string]interface{}, error)

ToMap encodes the input as a map

func UnixTime

func UnixTime() interface{}

UnixTime returns a timestamp in unix time

Types

type Context added in v0.3.0

type Context interface {
	// Funcs returns a list of special template functions of the form func(template.Context, arg1, arg2) interface{}
	Funcs() []Function
}

Context is a marker interface for a user-defined struct that is passed into the template engine (as context) and accessible in the exported template functions. Template functions can have the signature func(template.Context, arg1, arg2 ...) (string, error) and when functions like this are registered, the template engine will dynamically create and export a function of the form func(arg1, arg2...) (string, error) where the context instance becomes an out-of-band struct that can be mutated by functions. This in essence allows structured data as output of the template, in addition to a string from evaluating the template.

type Function added in v0.3.0

type Function struct {

	// Name is the function name to bind in the template
	Name string

	// Description provides help for the function
	Description string

	// Func is the reference to the actual function
	Func interface{}
}

Function contains the description of an exported template function

type Options

type Options struct {

	// SocketDir is the directory for locating the socket file for
	// a template URL of the form unix://socket_file/path/to/resource
	SocketDir string
}

Options contains parameters for customizing the behavior of the engine

type Template

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

Template is the templating engine

func NewTemplate

func NewTemplate(s string, opt Options) (*Template, error)

NewTemplate fetches the content at the url and returns a template. If the string begins with str:// as scheme, then the rest of the string is interpreted as the body of the template.

func NewTemplateFromBytes

func NewTemplateFromBytes(buff []byte, contextURL string, opt Options) (*Template, error)

NewTemplateFromBytes builds the template from buffer with a contextURL which is used to deduce absolute path of any 'included' templates e.g. {{ include "./another.tpl" . }}

func (*Template) AddFunc

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

AddFunc adds a new function to support in template

func (*Template) DefaultFuncs

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

DefaultFuncs returns a list of default functions for binding in the template

func (*Template) Execute

func (t *Template) Execute(output io.Writer, context interface{}) error

Execute is a drop-in replace of the execute method of template

func (*Template) Render

func (t *Template) Render(context interface{}) (string, error)

Render renders the template given the context

func (*Template) SetOptions

func (t *Template) SetOptions(opt Options) *Template

SetOptions sets the runtime flags for the engine

func (*Template) Validate

func (t *Template) Validate() (*Template, error)

Validate parses the template and checks for validity.

Jump to

Keyboard shortcuts

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