Documentation ¶
Index ¶
- func Register(funcMap template.FuncMap)
- func Set(v *Blocks) func(http.Handler) http.Handler
- type Blocks
- func (v *Blocks) DefaultLayout(layoutName string) *Blocks
- func (v *Blocks) Delims(left, right string) *Blocks
- func (v *Blocks) ExecuteTemplate(w io.Writer, tmplName, layoutName string, data interface{}) error
- func (v *Blocks) Extension(ext string) *Blocks
- func (v *Blocks) Extensions(ext string, parser ExtensionParser) *Blocks
- func (v *Blocks) Funcs(funcMap template.FuncMap) *Blocks
- func (v *Blocks) LayoutDir(relToDirLayoutDir string) *Blocks
- func (v *Blocks) LayoutFuncs(funcMap template.FuncMap) *Blocks
- func (v *Blocks) Load() error
- func (v *Blocks) LoadWithContext(ctx context.Context) error
- func (v *Blocks) Option(opt ...string) *Blocks
- func (v *Blocks) ParseTemplate(tmplName, layoutName string, data interface{}) (string, error)
- func (v *Blocks) PartialFunc(partialName string, data interface{}) (template.HTML, error)
- func (v *Blocks) Reload(b bool) *Blocks
- func (v *Blocks) RootDir(root string) *Blocks
- type ContextKeyType
- type ErrNotExist
- type ExtensionParser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
Register register a function map that will be available across all Blocks view engines. The values (functions) should be compatible with a standard html/template function, however as a special feature, the function input's can be a type of func(*Blocks) (fn interface{}) or func(*Blocks) template.FuncMap as well, so it can use the current engine's methods such as `ParseTemplate`. It's legal to override previous functions.
It is used like the `database/sql.Register` function.
Usage:
package myFuncsCollection1 func init() { blocks.Register(a_funcMap) } package myFuncsCollection2 func init() { blocks.Register(anothrer_funcMap) } package main import _ "myFuncsCollection1" import _ "myFuncsCollection2" func main() { views := blocks.New("./views") } Views contains the functions of both collections.
func Set ¶
Set returns a handler wrapper which sets the current view engine to this "v" Blocks. Useful when the caller needs multiple Blocks engine instances per group of routes. Note that this is entirely optional, the caller could just wrap a function of func(v *Blocks) and return a handler which will directly use it. See `Get` too.
Types ¶
type Blocks ¶
type Blocks struct { // Root, Templates and Layouts can be accessed after `Load`. Root *template.Template Templates, Layouts map[string]*template.Template // contains filtered or unexported fields }
Blocks is the main structure which holds the necessary information and options to parse and render templates. See `New` to initialize a new one.
func Get ¶
Get retrieves the associated Blocks view engine retrieved from the request's context. See `Set` too.
func New ¶
func New(fs interface{}) *Blocks
New returns a fresh Blocks engine instance. It loads the templates based on the given fs FileSystem (or string). By default the layout files should be located at "$rootDir/layouts" sub-directory (see `RootDir` method), change this behavior can be achieved through `LayoutDir` method before `Load/LoadContext`. To set a default layout name for an empty layout definition on `ExecuteTemplate/ParseTemplate` use the `DefaultLayout` method.
The user can customize various options through the Blocks methods. The user of this engine MUST call its `Load/LoadWithContext` method once before any call of `ExecuteTemplate` and `ParseTemplate`.
Global functions registered through `Register` package-level function will be inherited from this engine. To add a function map to this engine use its `Funcs` method.
The default extension can be changed through the `Extension` method. More extension parsers can be added through the `Extensions` method. The left and right delimeters can be customized through its `Delims` method. To reload templates on each request (useful for development stage) call its `Reload(true)` method.
Usage: New("./views") or New(http.Dir("./views")) or New(embeddedFS) or New(AssetFile()) for embedded data.
func (*Blocks) DefaultLayout ¶
DefaultLayout sets the "layoutName" to be used when the `ExecuteTemplate`'s one is empty.
func (*Blocks) Delims ¶
Delims sets the action delimiters to the specified strings, to be used in Load. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the engine, so calls can be chained.
func (*Blocks) ExecuteTemplate ¶
ExecuteTemplate applies the template associated with "tmplName" to the specified "data" object and writes the output to "w". 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.
If "layoutName" and "v.defaultLayoutName" are both empty then the template is executed without a layout.
A template may be executed safely in parallel, although if parallel executions share a Writer the output may be interleaved.
func (*Blocks) Extension ¶
Extension sets the template file extension (with dot). Defaults to ".html".
func (*Blocks) Extensions ¶
func (v *Blocks) Extensions(ext string, parser ExtensionParser) *Blocks
Extensions registers a parser that will be called right before a file's contents parsed as a template. The "ext" should start with dot (.), e.g. ".md". The "parser" is a function which accepts the original file's contents and should return the parsed ones, e.g. return markdown.Run(contents), nil.
The default underline map contains a single element of ".md": markdown.Run, which is responsible to convert markdown files to html right before its contents are given to the template's parser.
To override an extension handler pass a nil "parser".
func (*Blocks) Funcs ¶
Funcs adds the elements of the argument map to the root template's function map. It must be called before the engine is loaded. 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 engine, so calls can be chained.
The default function map contains a single element of "partial" which can be used to render templates directly.
func (*Blocks) LayoutDir ¶
LayoutDir sets a custom layouts directory, always relative to the "rootDir" one. Layouts are recognised by their prefix names. Defaults to "layouts".
func (*Blocks) LayoutFuncs ¶
LayoutFuncs same as `Funcs` but this map's elements will be added only to the layout templates. It's legal to override elements of the root `Funcs`.
func (*Blocks) Load ¶
Load parses the templates, including layouts, through the html/template standard package into the Blocks engine.
func (*Blocks) LoadWithContext ¶
LoadWithContext accepts a context that can be used for load cancelation, deadline/timeout. It parses the templates, including layouts, through the html/template standard package into the Blocks engine.
func (*Blocks) Option ¶
Option sets options for the templates. Options are described by strings, either a simple string or "key=value". There can be at most one equals sign in an option string. If the option string is unrecognized or otherwise invalid, Option panics.
Known options:
missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map.
"missingkey=default" or "missingkey=invalid" The default behavior: Do nothing and continue execution. If printed, the result of the index operation is the string "<no value>". "missingkey=zero" The operation returns the zero value for the map type's element. "missingkey=error" Execution stops immediately with an error.
func (*Blocks) ParseTemplate ¶
ParseTemplate parses a template based on its "tmplName" name and returns the result. Note that, this does not reload the templates on each call if Reload was set to true. To refresh the templates you have to manually call the `Load` upfront.
func (*Blocks) PartialFunc ¶
PartialFunc returns the parsed result of the "partialName" template's "content" block.
type ContextKeyType ¶
type ContextKeyType struct{}
ContextKeyType is the type which `Set` request's context value is using to store the current Blocks engine.
See `Set` and `Get`.
var ContextKey ContextKeyType
ContextKey is the request's context value for a blocks engine.
See `Set` and `Get`.
type ErrNotExist ¶
type ErrNotExist struct {
Name string
}
ErrNotExist reports whether a template was not found in the parsed templates tree.
func (ErrNotExist) Error ¶
func (e ErrNotExist) Error() string
Error implements the `error` interface.
type ExtensionParser ¶
ExtensionParser type declaration to customize other extension's parsers before passed to the template's one.
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
embedded-bindata
Package main generated by go-bindata.// sources: ../basic/views/500.html ../basic/views/index.html ../basic/views/layouts/error.html ../basic/views/layouts/main.html ../basic/views/partials/footer.html
|
Package main generated by go-bindata.// sources: ../basic/views/500.html ../basic/views/index.html ../basic/views/layouts/error.html ../basic/views/layouts/main.html ../basic/views/partials/footer.html |
funcs/mycollection
Package mycollection contains a template.FuncMap that should be registered across all Blocks view engines.
|
Package mycollection contains a template.FuncMap that should be registered across all Blocks view engines. |