Documentation ¶
Overview ¶
Package decor contains a simple-to-user yet powerful template loader mainly focussed on the golang built-in templates text/template and html/template.
While these packages provide powerful templating facilities with great support for rendering textual content, using these packages to build a complete layout-based template solution involves a lot of boilerplate code. decor tries to fill this gap by providing just what's needed to reduce the boilerplate and enable a great developer experience.
Example (Layout) ¶
package main import ( "log" "os" "github.com/halimath/decor" "github.com/halimath/decor/text" ) func main() { tpls := decor.Templates{ Includes: []string{ "layouts/base", }, Loader: text.NewFilesLoader("%s.txt", "testtemplates"), } if err := tpls.ExecuteTemplate(os.Stdout, "a", "world"); err != nil { log.Fatal(err) } }
Output: Hello, world!
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Loader ¶
type Loader interface { // Loads the templates named names and returns them. If funcs is a non-nil, non-empty map the contained // functions must be made available to the template. Returns a non-nil error to indicate that loading // failed. Load(names []string, funcs FuncMap) (Template, error) }
Loader defines the interface for implementations that load Template values from a source. Templates are loaded by name.
type Template ¶
type Template interface { // Execute executes the template rendering content using data and // writing the output to wr. It returns an error in case of an error // or nil to indicate success. Execute(wr io.Writer, data interface{}) error }
Template defines an internal abstraction for templates provided either by package text/template or html/template or a custom template rendering engine.
type Templates ¶
type Templates struct { // When set to true DevelMode puts these templates into development mode, which // causes errors during rendering being written to the target writer as well // as templates being reload on every execution. DevelMode bool // Funcs contains a map of functions to be used in templates. // These are passed to every template loaded using the given // Templates. Funcs FuncMap // Loader is used to load Templates when requested. Loader Loader // Includes contains a list of template names to load with every template, such as helper templates or // layouts. Includes []string // contains filtered or unexported fields }
Templates implements loading, caching and execution of named templates.
func (*Templates) ExecuteTemplate ¶
ExecuteTemplate executes the template named templateName with data and writes the output to w. It returns any non-nil error returned from the loader or from Template.Execute.
func (*Templates) SendHTML ¶
func (t *Templates) SendHTML(w http.ResponseWriter, templateName string, data interface{})
SendHTML executes the template named templateName with the given data (using Repository.ExecuteTemplate). It writes the rendered output to w after setting the responsive HTTP content headers. This method pays attention to the r's DevelMode: When activated any error produced from rendering the template will be send as the http response. When DevelopMode is false, any error will be suppressed possibly resulting in an empty response.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
gen-html
Main contains a helper application to generate the html/template based implementation by copying the text/template based sources.
|
Main contains a helper application to generate the html/template based implementation by copying the text/template based sources. |
Package text contains a layout based loader for text/template.
|
Package text contains a layout based loader for text/template. |
Package text contains a layout based loader for text/template.
|
Package text contains a layout based loader for text/template. |