Documentation ¶
Overview ¶
Example ¶
Basic usage using the templates Default templates.
Below one partial named 'base' will be used to wrap two views, 'home' and 'about'
package main import ( "os" "github.com/biz/templates" ) func main() { // Create a partial named base that will be used to wrap the contents of a view templates.AddPartial("base", ` <!DOCTYPE HTML> <html> <body> {{- block "header" .}} <header> I am the header that will wrap your view </header> {{- end }} {{- block "contents" . }}{{- end}} {{- block "footer" . }} <footer> I am the footer that will wrap your view </footer> {{- end }} </body> </html>`) // Create a view named home templates.AddView("home", ` {{- define "contents" }} <p> Hello from the home view </p> {{- end }} `) // Create a view named about templates.AddView("about", ` {{- define "header" }} <h1>About page with its own idea of what it wants in the header</h1> {{- end }} {{- define "contents" }} Hello from the about view {{- end }} `) // Parse the templates // NOTE: views and partiels should be created and parsed at startup. templates.Parse() // Render the home template wrapped by the base partial templates.MustExecute(os.Stdout, "base", "home", nil) // Render the about template wrapped by the base partial templates.MustExecute(os.Stdout, "base", "about", nil) }
Output: <!DOCTYPE HTML> <html> <body> <header> I am the header that will wrap your view </header> <p> Hello from the home view </p> <footer> I am the footer that will wrap your view </footer> </body> </html> <!DOCTYPE HTML> <html> <body> <h1>About page with its own idea of what it wants in the header</h1> Hello from the about view <footer> I am the footer that will wrap your view </footer> </body> </html>
Index ¶
- Variables
- func AddFunc(name string, f interface{})
- func AddFuncs(funcMap template.FuncMap)
- func AddPartial(name string, tmpl string)
- func AddView(name string, tmpl string)
- func Delims(left, right string)
- func Execute(w io.Writer, baseView, view string, data interface{}) error
- func ExecuteSingle(w io.Writer, view string, data interface{}) error
- func MustExecute(w io.Writer, baseView, view string, data interface{})
- func MustExecuteSingle(w io.Writer, view string, data interface{})
- func MustRender(baseView, view string, data interface{})
- func MustRenderSingle(view string, data interface{})
- func Parse()
- func Render(baseView, view string, data interface{}) ([]byte, error)
- func RenderSingle(view string, data interface{}) ([]byte, error)
- func UseExts(extensions []string)
- type Templates
- func (t *Templates) AddFunc(name string, f interface{})
- func (t *Templates) AddFuncs(funcMap template.FuncMap)
- func (t *Templates) AddPartial(name string, tmpl string)
- func (t *Templates) AddView(name string, tmpl string)
- func (t *Templates) Delims(left, right string)
- func (t *Templates) Execute(w io.Writer, baseView, view string, data interface{}) error
- func (t *Templates) ExecuteSingle(w io.Writer, view string, data interface{}) error
- func (t *Templates) MustExecute(w io.Writer, baseView, view string, data interface{})
- func (t *Templates) MustExecuteSingle(w io.Writer, view string, data interface{})
- func (t *Templates) MustRender(baseView, view string, data interface{}) []byte
- func (t *Templates) MustRenderSingle(view string, data interface{}) []byte
- func (t *Templates) Parse()
- func (t *Templates) ParseDir(dir string, stripPrefix string) (*Templates, error)
- func (t *Templates) ParseEmbed(files embed.FS, stripPrefix string) (*Templates, error)
- func (t *Templates) Render(baseView, view string, data interface{}) ([]byte, error)
- func (t *Templates) RenderSingle(view string, data interface{}) ([]byte, error)
- func (t *Templates) UseExts(extensions []string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var Default = New()
Default is used to contain the default templates instance
Functions ¶
func AddPartial ¶
func MustExecute ¶
func MustExecuteSingle ¶
func MustRender ¶
func MustRender(baseView, view string, data interface{})
func MustRenderSingle ¶
func MustRenderSingle(view string, data interface{})
func RenderSingle ¶
Types ¶
type Templates ¶
type Templates struct { Templates map[string]*template.Template Extensions map[string]bool // contains filtered or unexported fields }
func (*Templates) AddPartial ¶
func (*Templates) ExecuteSingle ¶
func (*Templates) MustExecute ¶
func (*Templates) MustExecuteSingle ¶
func (*Templates) MustRender ¶
func (*Templates) MustRenderSingle ¶
func (*Templates) ParseEmbed ¶
func (*Templates) RenderSingle ¶
Click to show internal directories.
Click to hide internal directories.