Documentation
¶
Overview ¶
Package mustache is an implementation of the mustache templating language in Go. For more information on mustache check out the official documentation at http://mustache.github.io.
temlpate := mustache.New() err := template.ParseString("Hello, {{subject}}!") if err != nil { // handle error } s, err := template.RenderString(map[string]string{"subject": "world"}) if err != nil { // handle error } fmt.Println(s)
There are several wrappers of Parse and Render to help with different input or output types. It is quite common to need to write the output of the template to an http.ResponseWriter. In this case the Render function is the most apropriate.
import "net/http" import "github.com/alexkappa/mustache" func ServeHTTP(w http.ResponseWriter, r *http.Request) { template, err := mustache.ParseString("Hello, {{subject}}!") if err != nil { // handle error } err = template.Render(w, map[string]string{"subject": "world"}) if err != nil { // handle error } }
Index ¶
- func Render(r io.Reader, w io.Writer, context ...interface{}) error
- type Option
- type Template
- func (t *Template) Option(options ...Option)
- func (t *Template) Parse(r io.Reader) error
- func (t *Template) ParseBytes(b []byte) error
- func (t *Template) ParseString(s string) error
- func (t *Template) Render(w io.Writer, context ...interface{}) error
- func (t *Template) RenderBytes(context ...interface{}) ([]byte, error)
- func (t *Template) RenderString(context ...interface{}) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(*Template)
The Option type describes functional options used with Templates. Check out Dave Cheney's talk on functional options http://bit.ly/1x9WWPi.
Example ¶
title := New(Name("header")) // instantiate and name the template title.ParseString("{{title}}") // parse a template string body := New() body.Option(Name("body")) // options can be defined after we instantiate too body.ParseString("{{content}}") template := New( Delimiters("|", "|"), // set the mustache delimiters to | instead of {{ SilentMiss(false), // return an error if a variable lookup fails Partial(title), // register a partial Partial(body)) // and another one... template.ParseString("|>header|\n|>body|") context := map[string]interface{}{ "title": "Mustache", "content": "Logic less templates with Mustache!", } template.Render(os.Stdout, context)
Output: Mustache Logic less templates with Mustache!
func Delimiters ¶
Delimiters sets the start and end delimiters of the template.
func Errors ¶
func Errors() Option
Errors enables missing variable errors. This option is deprecated. Please use SilentMiss instead.
func Partial ¶
Partial sets p as a partial to the template. It is important to set the name of p so that it may be looked up by the parent template.
func SilentMiss ¶
SilentMiss sets the silent miss behavior of variable lookups when rendering. If true, missed lookups will not produce any errors. Otherwise a missed variable lookup will stop the rendering and return an error.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
The Template type represents a template and its components.
Example (Basic) ¶
template := New() template.ParseString(`{{#foo}}{{bar}}{{/foo}}`) context := map[string]interface{}{ "foo": true, "bar": "bazinga!", } output, _ := template.RenderString(context) fmt.Println(output)
Output: bazinga!
Example (Http) ¶
writer := httptest.NewRecorder() request, _ := http.NewRequest("GET", "http://example.com?foo=bar&bar=one&bar=two", nil) template := New() template.ParseString(` <ul>{{#foo}}<li>{{.}}</li>{{/foo}}</ul> <ul>{{#bar}}<li>{{.}}</li>{{/bar}}</ul>`) handler := func(w http.ResponseWriter, r *http.Request) { template.Render(w, r.URL.Query()) } handler(writer, request) fmt.Println(writer.Body.String())
Output: <ul><li>bar</li></ul> <ul><li>one</li><li>two</li></ul>
Example (Partials) ¶
partial := New(Name("partial")) partial.ParseString(`{{bar}}`) template := New(Partial(partial)) template.ParseString(`{{#foo}}{{>partial}}{{/foo}}`) context := map[string]interface{}{ "foo": true, "bar": "bazinga!", } template.Render(os.Stdout, context)
Output: bazinga!
Example (Reader) ¶
f, err := os.Open("template.mustache") if err != nil { fmt.Fprintf(os.Stderr, "failed to open file: %s\n", err) } t, err := Parse(f) if err != nil { fmt.Fprintf(os.Stderr, "failed to parse template: %s\n", err) } t.Render(os.Stdout, nil)
Output:
func (*Template) Parse ¶
Parse parses a stream of bytes read from r and creates a parse tree that represents the template.
func (*Template) ParseBytes ¶
ParseBytes is a helper function that uses a byte array as input.
func (*Template) ParseString ¶
ParseString is a helper function that uses a string as input.
func (*Template) Render ¶
Render walks through the template's parse tree and writes the output to w replacing the values found in context.
func (*Template) RenderBytes ¶
RenderBytes is a helper function that renders the template as a byte slice.
func (*Template) RenderString ¶
RenderString is a helper function that renders the template as a string.