template

package
v0.0.0-...-55d16bb Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2017 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventMethodPrefix = "Event"
)
View Source
const (
	FuncMethodPrefix = "Func"
)
View Source
const (
	GlobalID = "global"
)
View Source
const (
	MustMethodPrefix = "Must"
)

Variables

View Source
var (

	// Custom error types.
	ErrNoFilesFound          = errors.New("bulldozer/template: no files named in call to ParseFiles")
	ErrPatternMatchesNoFiles = errors.New("bulldozer/template: pattern matches no files!")
)
View Source
var (
	ExecTemplateAbort = errors.New("Template execution aborted")
)

Functions

func DisableSessionContextStore

func DisableSessionContextStore(s *sessions.Session)

DisableSessionContextStore disables the context store for the session.

func EnableSessionContextStore

func EnableSessionContextStore(s *sessions.Session)

EnableSessionContextStore enables the context store for the session. All current active contexts of the session will be saved to the sessions instance values. This adds some overhead during each render request. Only activate this if you need access to the sessions contexts.

func ExecuteContext

func ExecuteContext(c *Context, wr io.Writer, data interface{}) error

ExecuteContext executes the template context.

func RegisterPackage

func RegisterPackage(name string, i interface{})

RegisterPackage registeres a new template package. This call is not thread-safe! Register packages during program initialization. A template package function has the following syntax: func (p *Package) MustIsAuth(a *template.Action, c *template.Context) {}

func RegisterPlugin

func RegisterPlugin(p Plugin, opts *PluginOpts) error

RegisterPlugin adds and registers a template plugin with the given type. This method call is not thread-safe!

func ResetEnvironment

func ResetEnvironment(s *sessions.Session)

ResetEnvironment releases all registered session events. All previously registered client event calls will be invalid. If a context store is present, then this is also resetted.

func TriggerGlobalEvent

func TriggerGlobalEvent(s *sessions.Session, eventName string, params ...interface{})

TriggerGlobalEvent triggers the global event on the client side defined with the template event syntax.

Types

type Action

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

func (*Action) Error

func (a *Action) Error(errorMessage string)

func (*Action) Redirect

func (a *Action) Redirect(url string)

type Args

type Args map[string]string

type Context

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

func (*Context) Delete

func (c *Context) Delete(key interface{})

Delete removes the execution value with the given key. This operation is thread-safe.

func (*Context) DomID

func (c *Context) DomID() string

DomID returns the DOM ID of the current context.

func (*Context) GenDomID

func (c *Context) GenDomID(id string) string

GenDomID generates the real DOM ID of id. This is equivalent to the following template call: {{id "YOUR_ID"}}

func (*Context) Get

func (c *Context) Get(key interface{}, vars ...func() interface{}) (value interface{}, ok bool)

Get obtains the execution value. Execution values exist for one complete execution cycle. A single variadic argument is accepted, and it is optional: if a function is set, this function will be called if no value exists for the given key. This operation is thread-safe.

func (*Context) ID

func (c *Context) ID() string

ID returns the unique ID of this execution context. Use this for example as database access keys...

func (*Context) New

func (c *Context) New(t *Template, id string, vars ...[]string) *Context

New creates a new sub context. One optional slice can be passed, which defines additional style classes.

func (*Context) Pull

func (c *Context) Pull(key interface{}, vars ...func() interface{}) (interface{}, bool)

Pull does the same as Get(), but additionally removes the value from the map if present. Use this for Flash values...

func (*Context) Release

func (c *Context) Release()

Release removes all session template events and releases the current context.

func (*Context) RootID

func (c *Context) RootID() string

RootID returns the unique ID of the root template.

func (*Context) Session

func (c *Context) Session() *sessions.Session

Session resturns the current context session.

func (*Context) Set

func (c *Context) Set(key interface{}, value interface{})

Set sets the execution value with the given key. This operation is thread-safe.

func (*Context) StoreDelete

func (c *Context) StoreDelete(key interface{})

StoreDelete removes the store value with the given key. This operation is thread-safe.

func (*Context) StoreGet

func (c *Context) StoreGet(key interface{}, vars ...func() interface{}) (value interface{}, ok bool)

StoreGet obtains the store value, which are stored in the session store. They survive application restarts. Values have to be encodable by gob. Don't store pointers! A single variadic argument is accepted, and it is optional: if a function is set, this function will be called if no value exists for the given key. This operation is thread-safe.

func (*Context) StorePull

func (c *Context) StorePull(key interface{}, vars ...func() interface{}) (interface{}, bool)

StorePull does the same as Get(), but additionally removes the value from the store if present. Use this for Flash values...

func (*Context) StoreSet

func (c *Context) StoreSet(key interface{}, value interface{})

StoreSet sets the store value with the given key. This operation is thread-safe.

func (*Context) Styles

func (c *Context) Styles() []string

Styles returns a slice of all template styles.

func (*Context) StylesString

func (c *Context) StylesString() (str string)

StylesString returns a string of all template styles.

func (*Context) Template

func (c *Context) Template() *Template

Template returns the current context template.

func (*Context) TriggerEvent

func (c *Context) TriggerEvent(eventName string, params ...interface{})

TriggerEvent triggers the event on the client side defined with the template event syntax.

func (*Context) Update

func (c *Context) Update(vars ...interface{}) error

Update executes the template and updates the new DOM content. One optional argument can be passed. This is the render data for the template. If omitted, the data is obtained through the template.OnGetData function.

type ContextStore

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

func GetSessionContextStore

func GetSessionContextStore(s *sessions.Session) *ContextStore

GetSessionContextStore returns the session context store if present or nil.

func (*ContextStore) Get

func (cs *ContextStore) Get(id string) (*Context, bool)

Get a context from the store by it's ID if present.

type ExecOpts

type ExecOpts struct {
	Data         interface{} // If the data is passed with the execute method call, then the onGetData function is not called.
	ID           string      // This is added to the unique context ID.
	DomID        string      // Set this, to set a custom DOM ID.
	StyleClasses []string    // Additional style classes.
}

type FuncMap

type FuncMap map[string]interface{}

FuncMap is the type of the map defining the mapping from names to functions. Each function must have either a single return value, or two return values of which the second has type error. In that case, if the second (error) argument evaluates to non-nil during execution, execution terminates and Execute returns that error. FuncMap has the same base type as FuncMap in "text/template", copied here so clients need not import "text/template".

type GetDataFunc

type GetDataFunc func(c *Context) interface{}

GetDataFunc is called during template execution if no data is specified with the optional execute option. Return the template render data.

type Plugin

type Plugin interface {
	Prepare(d *PluginData) error // Prepare is called for each plugin context. Settings should be parsed...
	Render(c *Context, d *PluginData) (interface{}, error)
}

func GetPlugin

func GetPlugin(typeStr string) Plugin

GetPlugin returns the plugin defined by the type. if not found, nil is returned.

type PluginData

type PluginData struct {
	Value   interface{}
	Args    Args
	Section string
}

type PluginOpts

type PluginOpts struct {
	Type       string // The plugin type.
	HasSection bool   // If the plugin requires a template section.
	RequireID  bool   // If the plugin requires a unique ID as argument.
}

type Template

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

func Must

func Must(t *Template, err error) *Template

Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil. It is intended for use in variable initializations such as

var t = template.Must(template.New("name").Parse("html"))

func New

func New(uid string, name string) *Template

New allocates a new bulldozer template with the given name. The uid has to be a unique ID for this new template set. This uid is used to access templates over events even during a bulldozer application restart...

func ParseFiles

func ParseFiles(uid string, filenames ...string) (*Template, error)

ParseFiles creates a new Template and parses the template definitions from the named files. The returned template's name will have the (base) name and (parsed) contents of the first file. There must be at least one file. If an error occurs, parsing stops and the returned *Template is nil.

func ParseGlob

func ParseGlob(uid string, pattern string) (*Template, error)

ParseGlob creates a new Template and parses the template definitions from the files identified by the pattern, which must match at least one file. The returned template will have the (base) name and (parsed) contents of the first file matched by the pattern. ParseGlob is equivalent to calling ParseFiles with the list of files matched by the pattern.

func ParseRec

func ParseRec(uid string, dir string, excludeDirs ...string) (*Template, error)

ParseRec parses all template files recursivly in the given directory path.

func ParseRecToNamespace

func ParseRecToNamespace(uid string, namespace string, dir string, excludeDirs ...string) (*Template, error)

ParseRec parses all template files recursivly in the given directory path. The namespace is prepended to the template name. namespace: bulldozer template name: bulldozer/templatename

func (*Template) AddStyleClass

func (t *Template) AddStyleClass(styleClass string) *Template

AddStyleClass adds a style class to the template div. The return value is the template, so calls can be chained.

func (*Template) Delims

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

Delims sets the action delimiters to the specified strings, to be used in subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the template, so calls can be chained.

func (*Template) Execute

func (t *Template) Execute(s *sessions.Session, wr io.Writer, optArgs ...ExecOpts) (c *Context, err error)

Execute applies a parsed template to the specified data object, writing the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel. Optional options can be passed.

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(s *sessions.Session, wr io.Writer, name string, optArgs ...ExecOpts) (c *Context, found bool, err error)

ExecuteTemplate applies the template associated with t that has the given name to the specified data object and writes the output to wr. If an error occurs executing the template or writing its output, execution stops, but partial results may already have been written to the output writer. A template may be executed safely in parallel. A boolean is returned, defining if the template exists... Optional options can be passed.

func (*Template) ExecuteTemplateToString

func (t *Template) ExecuteTemplateToString(s *sessions.Session, name string, optArgs ...ExecOpts) (string, *Context, bool, error)

ExecuteTemplateToString does the same as ExecuteTemplate, but instead writes the output to a string.

func (*Template) ExecuteToString

func (t *Template) ExecuteToString(s *sessions.Session, optArgs ...ExecOpts) (string, *Context, error)

ExecuteToString does the same as Execute, but instead writes the output to a string.

func (*Template) Funcs

func (t *Template) Funcs(funcMap FuncMap) *Template

Funcs adds the elements of the argument map to the template's function map. It panics if a value in the map is not a function with appropriate return type. However, it is legal to overwrite elements of the map. The return value is the template, so calls can be chained. Functions have to be registered before any template which use these functions are parsed. This method is equivalent of calling the html/template Funcs method.

func (*Template) Lookup

func (t *Template) Lookup(name string) *Template

Lookup returns the template with the given name that is associated with t, or nil if there is no such template.

func (*Template) LookupFatal

func (t *Template) LookupFatal(name string) (tt *Template)

LookupFatal fatals if the template does not exists with the given name.

func (*Template) LookupMust

func (t *Template) LookupMust(name string) (tt *Template)

LookupMust panics if the template does not exists with the given name.

func (*Template) Name

func (t *Template) Name() string

Name returns the name of the template.

func (*Template) New

func (t *Template) New(name string) *Template

New allocates a new bulldozer template associated with the given one and with the same delimiters. The association, which is transitive, allows one template to invoke another with a {{template}} action.

func (*Template) OffTemplateExecution

func (t *Template) OffTemplateExecution(f func(c *Context, data interface{}))

OffTemplateExecution removes the listener again

func (*Template) OffTemplateExecutionFinished

func (t *Template) OffTemplateExecutionFinished(f func(c *Context, data interface{}))

OffTemplateExecutionFinished removes the listener again

func (*Template) OnGetData

func (t *Template) OnGetData(f GetDataFunc) *Template

OnGetData set the function which is called during template execution if no data is specified with the optional execute option. Return the template render data.

func (*Template) OnTemplateExecution

func (t *Template) OnTemplateExecution(f func(c *Context, data interface{}))

OnTemplateExecution is triggered during each template execution

func (*Template) OnTemplateExecutionFinished

func (t *Template) OnTemplateExecutionFinished(f func(c *Context, data interface{}))

OnTemplateExecutionFinished is triggered after each template execution

func (*Template) OnceTemplateExecution

func (t *Template) OnceTemplateExecution(f func(c *Context, data interface{}))

OnceTemplateExecution is the same event as OnTemplateExecution, but the listener is triggered only once

func (*Template) OnceTemplateExecutionFinished

func (t *Template) OnceTemplateExecutionFinished(f func(c *Context, data interface{}))

OnceTemplateExecutionFinished is the same event as OnTemplateExecutionFinished, but the listener is triggered only once

func (*Template) Parse

func (t *Template) Parse(src string) (tt *Template, err error)

Parse parses a string into a template. Nested template definitions will be associated with the top-level template t. Parse may be called multiple times to parse definitions of templates to associate with t. It is an error if a resulting template is non-empty (contains content other than template definitions) and would replace a non-empty template with the same name. (In multiple calls to Parse with the same receiver template, only one call can contain text other than space, comments, and template definitions.)

func (*Template) ParseFiles

func (t *Template) ParseFiles(filenames ...string) (*Template, error)

ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file.

func (*Template) ParseGlob

func (t *Template) ParseGlob(pattern string) (*Template, error)

ParseGlob parses the template definitions in the files identified by the pattern and associates the resulting templates with t. The pattern is processed by filepath.Glob and must match at least one file. ParseGlob is equivalent to calling t.ParseFiles with the list of files matched by the pattern.

func (*Template) ParseRec

func (t *Template) ParseRec(dir string, excludeDirs ...string) (*Template, error)

ParseRec parses all template files recursivly in the given directory path.

func (*Template) ParseRecToNamespace

func (t *Template) ParseRecToNamespace(namespace string, dir string, excludeDirs ...string) (*Template, error)

ParseRec parses all template files recursivly in the given directory path. The namespace is prepended to the template name. namespace: bulldozer template name: bulldozer/templatename

func (*Template) RegisterEvents

func (t *Template) RegisterEvents(i interface{}, vars ...string) *Template

RegisterEvents registeres the event methods of the interface. Event method names have to start with the 'Event' prefix. They are called from the client-side without this prefix. One optional parameter can be set, to define the events namespace. If no namespace is defined, then the events are registered in the global namespace.

func (*Template) RegisterFuncs

func (t *Template) RegisterFuncs(i interface{}, vars ...string) *Template

RegisterFuncs registeres the template function methods of the interface. Template function method names have to start with the 'Func' prefix. They are called from the template without this prefix. One optional parameter can be set, to define the functions namespace. If no namespace is defined, then the functions are registered in the global namespace. Only call this method before any template execution. This is not thread-safe.

The first parameter of a template function has to be a template Context pointer. Valid return values are following:

  • no return values
  • error
  • (interface{}, error)

func (*Template) SetStaticDomID

func (t *Template) SetStaticDomID(id string) *Template

SetStaticDomID sets a static DOM ID instead of using an automatic generated one. The return value is the template, so calls can be chained.

func (*Template) Templates

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

Templates returns a slice of the templates associated with t, including t itself.

func (*Template) UID

func (t *Template) UID() string

UID returns the unique ID of the template.

Jump to

Keyboard shortcuts

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