Documentation ¶
Overview ¶
Package temple is a library for managing go templates which supports sharing templates between a client and server. It is the library that powers the temple command line tool. Temple provides lets you load templates from files or load inline templates from the DOM. It also optionally organizes templates into regular templates, partials, and layouts, associating each template with other templates depending on which category it belongs to. Temple is compatible with gopherjs and can be compiled to javascript and run in a browser.
Version 0.1.3
Index ¶
- Variables
- func Build(src, dest, partials, layouts, packageName string) error
- func ExecuteEl(e Executor, el dom.Element, data interface{}) error
- type Executor
- type Group
- func (g *Group) AddAllFiles(templatesDir, partialsDir, layoutsDir string) error
- func (g *Group) AddAllInline() error
- func (g *Group) AddFunc(name string, f interface{})
- func (g *Group) AddInlineLayout(el dom.Element) error
- func (g *Group) AddInlinePartial(el dom.Element) error
- func (g *Group) AddInlineTemplate(el dom.Element) error
- func (g *Group) AddLayout(name, src string) error
- func (g *Group) AddLayoutFile(name, filename string) error
- func (g *Group) AddLayoutFiles(dir string) error
- func (g *Group) AddPartial(name, src string) error
- func (g *Group) AddPartialFile(name, filename string) error
- func (g *Group) AddPartialFiles(dir string) error
- func (g *Group) AddTemplate(name, src string) error
- func (g *Group) AddTemplateFile(name, filename string) error
- func (g *Group) AddTemplateFiles(dir string) error
- func (g Group) GetLayout(name string) (*Layout, error)
- func (g Group) GetPartial(name string) (*Partial, error)
- func (g Group) GetTemplate(name string) (*Template, error)
- func (g Group) MustGetLayout(name string) *Layout
- func (g Group) MustGetPartial(name string) *Partial
- func (g Group) MustGetTemplate(name string) *Template
- type Layout
- type Partial
- type Template
Constants ¶
This section is empty.
Variables ¶
var ( // PartialPrefix is added to the name of all partials. PartialPrefix = "partials/" // LayoutPrefix is added to the name of all layouts. LayoutPrefix = "layouts/" )
Functions ¶
func Build ¶
Build is the function called when you run the build sub-command in the command line tool. It compiles all the templates in the src directory and generates go code in the dest file. If partials and/or layouts are provided, it will add them to the generated file with calls to AddPartial and AddLayout. If packageName is an empty string, the package name will be the directory of the dest file.
Types ¶
type Executor ¶
Executor represents some type of template that is capable of executing (i.e. rendering) to an io.Writer with some data. It is satisfied by Template, Partial, and Layout as well as the builtin template.Template.
type Group ¶
type Group struct { // Funcs is a map of function names to functions. All functions in the // FuncMap are accessible by all templates, partials, and layouts for // this Group. Funcs template.FuncMap // contains filtered or unexported fields }
A Group represents a set of associated templates, partials, and layouts. Each Group also gets its own template.FuncMap called Funcs.
func (*Group) AddAllFiles ¶
AddAllFiles adds the .tmpl files located in templatesDir, partialsDir, and layoutsDir to the group as regular templates, partials, and layouts, respectively. It also adds the needed associations. The name assigned to each template, partial, or layout is based on the filename and the path relative to dir, just as it is in AddTemplateFiles, AddPartialFiles, and AddLayoutFiles, respectively.
func (*Group) AddAllInline ¶
AddAllInline scans the DOM for inline templates which must be script tags with the type "text/template". The id property will be used for the name of each template, and the special property "data-kind" can be used to distinguish between regular templates, partials, and layouts. So, to declare an inline partial for use with the AddAllInline method, use an opening script tag that looks like:
<script type="text/template" id="todo" data-kind="partial">
func (*Group) AddFunc ¶
AddFunc adds f to the FuncMap for the group under the given name. You must call AddFunc before adding any templates, partials, or layouts. Once added, all templates can call the function directly. See http://golang.org/pkg/text/template/ for more information about the FuncMap type and how to call functions from inside templates.
func (*Group) AddInlineLayout ¶
AddInlineLayout adds the inline template el to the group as a layout. It uses the id property as the template name and the innerHTML as the template source. LayoutsPrefix will be added to the name, which by default is "layouts/". Typically inline templates will be in script tags that look like:
<script type="text/template" id="app">
which would correspond to a template with the name "layouts/app".
func (*Group) AddInlinePartial ¶
AddInlinePartial adds the inline template el to the group as a partial. It uses the id property as the template name and the innerHTML as the template source. PartialsPrefix will be added to the name, which by default is "partials/". Typically inline templates will be in script tags that look like:
<script type="text/template" id="todo">
which would correspond to a template with the name "partials/todo".
func (*Group) AddInlineTemplate ¶
AddInlineTemplate adds the inline template el to the group as a regular template. It uses the id property as the template name and the innerHTML as the template source. Typically inline templates will be in script tags that look like:
<script type="text/template" id="home">
func (*Group) AddLayoutFile ¶
AddLayoutFile reads the contents of filename and adds a layout to the group using the given name and the contents of the file as the source.
func (*Group) AddLayoutFiles ¶
AddLayoutFiles recursively adds all the .tmpl files in dir and its subdirectories to the group. The name assigned to each layout is based on the filename and the path relative to dir. LayoutsPrefix is prepended to the name, which by default is "layouts/". So if dir is `my-layouts`, the file located at `my-layouts/app.tmpl` will be given the name "layouts/app".
func (*Group) AddPartial ¶
AddPartial adds a partial to the group with the given name and source.
func (*Group) AddPartialFile ¶
AddPartialFile reads the contents of filename and adds a partial to the group using the given name and the contents of the file as the source.
func (*Group) AddPartialFiles ¶
AddPartialFiles recursively adds all the .tmpl files in dir and its subdirectories to the group. The name assigned to each partial is based on the filename and the path relative to dir. PartialsPrefix is prepended to the name, which by default is "partials/". So if dir is `my-partials`, the file located at `my-partials/head.tmpl` will be given the name "partials/head".
func (*Group) AddTemplate ¶
AddTemplate adds a regular template to the group with the given name and source.
func (*Group) AddTemplateFile ¶
AddTemplateFile reads the contents of filename and adds a template to the group using the given name and the contents of the file as the source.
func (*Group) AddTemplateFiles ¶
AddTemplateFiles recursively adds all the .tmpl files in dir and its subdirectories to the group. The name assigned to each template is based on the filename and the path relative to dir. So if dir is `templates`, the file located at `templates/people/show.tmpl` will be given the name "people/show".
func (Group) GetLayout ¶ added in v0.1.0
GetLayout returns the layout identified by name, or an error if the layout could not be found.
func (Group) GetPartial ¶ added in v0.1.0
GetPartial returns the partial identified by name, or an error if the partial could not be found.
func (Group) GetTemplate ¶ added in v0.1.0
GetTemplate returns the template identified by name, or an error if the template could not be found.
func (Group) MustGetLayout ¶ added in v0.1.0
MustGetLayout works like GetLayout, except that it panics instead of returning an error if the layout could not be found.
func (Group) MustGetPartial ¶ added in v0.1.0
MustGetPartial works like GetPartial, except that it panics instead of returning an error if the partial could not be found.
func (Group) MustGetTemplate ¶ added in v0.1.0
MustGetTemplate works like GetTemplate, except that it panics instead of returning an error if the template could not be found.
type Layout ¶
Layout is a lightweight wrapper around template.Template from the builtin html/template package. It has all the same methods, plus some additional ones. Layouts are assiciated with all regular temlates, and partials are associated with layouts. That means you can render a partial inside of a lyout with the `template` action. You can also render a layout from inside a regular template with the `template` action. See http://golang.org/pkg/text/template/ for more information about nested templates and the `template` action.
func (*Layout) ExecuteEl ¶
ExecuteEl executes the layout with the given data and then writes the result to the innerHTML of el. It only works if you have compiled this code to javascript with gopherjs and it is running in a browser.
func (Layout) PrefixedName ¶
PrefixedName returns the name of the layout with LayoutsPrefix included. By default, LayoutsPrefix is "layouts/". Each layout can be rendered by any template using the `template` action with the prefixed name as the first argument.
type Partial ¶
Partial is a lightweight wrapper around template.Template from the builtin html/template package. It has all the same methods, plus some additional ones. Partials are associated with all other partials, as well as with regular templates and layouts. That means you can render partials inside of templates, layouts, or other partials with the `template` action. See http://golang.org/pkg/text/template/ for more information about nested templates and the `template` action.
func (*Partial) ExecuteEl ¶
ExecuteEl executes the partial with the given data and then writes the result to the innerHTML of el. It only works if you have compiled this code to javascript with gopherjs and it is running in a browser.
func (Partial) PrefixedName ¶
PrefixedName returns the name of the partial with PartialsPrefix included. By default, PartialsPrefix is "partials/". Each partial can be rendered inside any template, layout, or other partial using the `template` action with the prefixed name as the first argument.
type Template ¶
Template is a lightweight wrapper around template.Template from the builtin html/template package. It has all the same methods, plus some additional ones. All partials and layouts are associated with all templates, i.e. added to the parse tree for all templates. So you can render partials and layouts inside of a template with the `template` action. See http://golang.org/pkg/text/template/ for more information about nested templates and the `template` action.