Documentation ¶
Overview ¶
Package template provides the easier way to use templates via template engine supports multiple template engines with different file extensions 6 template engines supported: standard html/template amber django handlebars pug(jade) markdown
Index ¶
- Constants
- Variables
- func ExecuteRaw(src string, wr io.Writer, binding interface{}) error
- func ExecuteRawString(src string, binding interface{}) (result string, err error)
- func ExecuteString(name string, binding interface{}, options ...map[string]interface{}) (result string, err error)
- func ExecuteWriter(out io.Writer, name string, binding interface{}, ...) (err error)
- func GetCharsetOption(defaultValue string, options map[string]interface{}) string
- func GetGzipOption(defaultValue bool, options map[string]interface{}) bool
- func Load() error
- type BinaryLoader
- type Engine
- type EngineFuncs
- type EngineRawExecutor
- type Entries
- type Entry
- type Loader
- type Mux
- func (m *Mux) AddEngine(e Engine) *Loader
- func (m *Mux) ExecuteRaw(src string, wr io.Writer, binding interface{}) error
- func (m *Mux) ExecuteRawString(src string, binding interface{}) (result string, err error)
- func (m *Mux) ExecuteString(name string, binding interface{}, options ...map[string]interface{}) (result string, err error)
- func (m *Mux) ExecuteWriter(out io.Writer, name string, binding interface{}, ...) (err error)
- func (m *Mux) Load() error
Constants ¶
const NoLayout = "@.|.@no_layout@.|.@" // html/html.go. handlebars/handlebars.go. It's the same but the var is separated care for the future here.
NoLayout to disable layout for a particular template file
const (
// Version current version number
Version = "0.0.3"
)
Variables ¶
var ( // DefaultExtension the default file extension if empty setted DefaultExtension = ".html" // DefaultDirectory the default directory if empty setted DefaultDirectory = "." + string(os.PathSeparator) + "templates" )
Functions ¶
func ExecuteRaw ¶
ExecuteRaw read moreon template.go:EngineRawParser parse with the first valid EngineRawParser
func ExecuteRawString ¶
ExecuteRawString receives raw template source contents and returns it's result as string
func ExecuteString ¶
func ExecuteString(name string, binding interface{}, options ...map[string]interface{}) (result string, err error)
ExecuteString executes a template from a specific template engine and returns its contents result as string, it doesn't renders
func ExecuteWriter ¶
func ExecuteWriter(out io.Writer, name string, binding interface{}, options ...map[string]interface{}) (err error)
ExecuteWriter calls the correct template Engine's ExecuteWriter func
func GetCharsetOption ¶
GetCharsetOption receives a default value and the render options map and returns the correct charset for this render action
func GetGzipOption ¶
GetGzipOption receives a default value and the render options map and returns if gzip is enabled for this render action
Types ¶
type BinaryLoader ¶
type BinaryLoader struct {
*Loader
}
BinaryLoader optionally, called after EngineLocation's Directory, used when files are distrubuted inside the app executable sets the AssetFn and NamesFn
type Engine ¶
type Engine interface { // LoadDirectory builds the templates, usually by directory and extension but these are engine's decisions LoadDirectory(directory string, extension string) error // LoadAssets loads the templates by binary // assetFn is a func which returns bytes, use it to load the templates by binary // namesFn returns the template filenames LoadAssets(virtualDirectory string, virtualExtension string, assetFn func(name string) ([]byte, error), namesFn func() []string) error // ExecuteWriter finds, execute a template and write its result to the out writer // options are the optional runtime options can be passed by user and catched by the template engine when render // an example of this is the "layout" or "gzip" option ExecuteWriter(out io.Writer, name string, binding interface{}, options ...map[string]interface{}) error }
Engine the interface that all template engines must implement
type EngineFuncs ¶
type EngineFuncs interface { // Funcs should returns the context or the funcs, // this property is used in order to register any optional helper funcs Funcs() map[string]interface{} }
EngineFuncs is optional interface for the Engine used to insert the helper funcs
type EngineRawExecutor ¶
type EngineRawExecutor interface { // ExecuteRaw is super-simple function without options and funcs, it's not used widely ExecuteRaw(src string, wr io.Writer, binding interface{}) error }
EngineRawExecutor is optional interface for the Engine used to receive and parse a raw template string instead of a filename
type Entries ¶
type Entries []*Entry
Entries the template Engines with their loader
type Entry ¶
Entry contains a template Engine and its Loader
func (*Entry) LoadEngine ¶
LoadEngine loads the Engine using its registered loader Internal Note: Loader can be used without a mux because of this we have this type of function here which just pass itself's field into other itself's field which, normally, is not a smart choice.
type Loader ¶
type Loader struct { Dir string Extension string // AssetFn and NamesFn used when files are distrubuted inside the app executable AssetFn func(name string) ([]byte, error) NamesFn func() []string }
Loader contains the funcs to set the location for the templates by directory or by binary
func NewLoader ¶
func NewLoader() *Loader
NewLoader returns a default Loader which is used to load template engine(s)
func (*Loader) Directory ¶
func (t *Loader) Directory(dir string, fileExtension string) *BinaryLoader
Directory sets the directory to load from returns the Binary location which is optional
func (*Loader) IsBinary ¶
IsBinary returns true if .Binary is called and AssetFn and NamesFn are setted
func (*Loader) LoadEngine ¶
LoadEngine receives a template Engine and calls its LoadAssets or the LoadDirectory with the loader's locations
type Mux ¶
type Mux struct { // Reload reloads the template engine on each execute, used when the project is under development status // if true the template will reflect the runtime template files changes // defaults to false Reload bool // Entries the template Engines with their loader Entries Entries SharedFuncs map[string]interface{} // contains filtered or unexported fields }
Mux is an optional feature, used when you want to use multiple template engines It stores the loaders with each of the template engine, the identifier of each template engine is the (loader's) Extension the registry finds the correct template engine and executes the template so you can use and render a template file by it's file extension
func NewMux ¶
NewMux returns a new Mux Mux is an optional feature, used when you want to use multiple template engines It stores the loaders with each of the template engine, the identifier of each template engine is the (loader's) Extension the registry finds the correct template engine and executes the template so you can use and render a template file by it's file extension
func (*Mux) ExecuteRaw ¶
ExecuteRaw read moreon template.go:EngineRawParser parse with the first valid EngineRawParser
func (*Mux) ExecuteRawString ¶
ExecuteRawString receives raw template source contents and returns it's result as string
func (*Mux) ExecuteString ¶
func (m *Mux) ExecuteString(name string, binding interface{}, options ...map[string]interface{}) (result string, err error)
ExecuteString executes a template from a specific template engine and returns its contents result as string, it doesn't renders
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
nethttp_html
Package main is a small example for html/template, same for all other
|
Package main is a small example for html/template, same for all other |
nethttp_multi
Package main uses the template.Mux here to simplify the steps and support of loading more than one template engine for a single app you can do the same things without the Mux, as you saw on the /html example folder
|
Package main uses the template.Mux here to simplify the steps and support of loading more than one template engine for a single app you can do the same things without the Mux, as you saw on the /html example folder |
Package handlebars the HandlebarsEngine's functionality
|
Package handlebars the HandlebarsEngine's functionality |
Package pug the JadeEngine's functionality lives inside ../html now
|
Package pug the JadeEngine's functionality lives inside ../html now |