Documentation ¶
Overview ¶
Package raymond provides handlebars evaluation
Example ¶
source := "<h1>{{title}}</h1><p>{{body.content}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // evaluate template with context output := tpl.MustExec(ctx) // alternatively, for one shots: // output := MustRender(source, ctx) fmt.Print(output)
Output: <h1>foo</h1><p>bar</p>
Example (Struct) ¶
source := `<div class="post"> <h1>By {{fullName author}}</h1> <div class="body">{{body}}</div> <h1>Comments</h1> {{#each comments}} <h2>By {{fullName author}}</h2> <div class="body">{{content}}</div> {{/each}} </div>` type Person struct { FirstName string LastName string } type Comment struct { Author Person Body string `handlebars:"content"` } type Post struct { Author Person Body string Comments []Comment } ctx := Post{ Person{"Jean", "Valjean"}, "Life is difficult", []Comment{ Comment{ Person{"Marcel", "Beliveau"}, "LOL!", }, }, } RegisterHelper("fullName", func(person Person) string { return person.FirstName + " " + person.LastName }) output := MustRender(source, ctx) fmt.Print(output)
Output: <div class="post"> <h1>By Jean Valjean</h1> <div class="body">Life is difficult</div> <h1>Comments</h1> <h2>By Marcel Beliveau</h2> <div class="body">LOL!</div> </div>
Index ¶
- Variables
- func Escape(s string) string
- func IsTrue(obj interface{}) bool
- func MustRender(source string, ctx interface{}) string
- func RegisterHelper(name string, helper interface{})
- func RegisterHelpers(helpers map[string]interface{})
- func RegisterPartial(name string, source string)
- func RegisterPartialTemplate(name string, tpl *Template)
- func RegisterPartials(partials map[string]string)
- func RemoveAllHelpers()
- func RemoveAllPartials()
- func RemoveHelper(name string)
- func RemovePartial(name string)
- func Render(source string, ctx interface{}) (string, error)
- func Str(value interface{}) string
- type DataFrame
- type Options
- func (options *Options) Ctx() interface{}
- func (options *Options) Data(name string) interface{}
- func (options *Options) DataFrame() *DataFrame
- func (options *Options) DataStr(name string) string
- func (options *Options) Eval(ctx interface{}, field string) interface{}
- func (options *Options) Fn() string
- func (options *Options) FnCtxData(ctx interface{}, data *DataFrame) string
- func (options *Options) FnData(data *DataFrame) string
- func (options *Options) FnWith(ctx interface{}) string
- func (options *Options) Hash() map[string]interface{}
- func (options *Options) HashProp(name string) interface{}
- func (options *Options) HashStr(name string) string
- func (options *Options) Inverse() string
- func (options *Options) NewDataFrame() *DataFrame
- func (options *Options) Param(pos int) interface{}
- func (options *Options) ParamStr(pos int) string
- func (options *Options) Params() []interface{}
- func (options *Options) Value(name string) interface{}
- func (options *Options) ValueStr(name string) string
- type Partial
- type SafeString
- type Template
- func (tpl *Template) Clone() *Template
- func (tpl *Template) Exec(ctx interface{}) (result string, err error)
- func (tpl *Template) ExecWith(ctx interface{}, privData *DataFrame) (result string, err error)
- func (tpl *Template) MustExec(ctx interface{}) string
- func (tpl *Template) PrintAST() string
- func (tpl *Template) RegisterHelper(name string, helper interface{})
- func (tpl *Template) RegisterHelpers(helpers map[string]interface{})
- func (tpl *Template) RegisterPartial(name string, source string)
- func (tpl *Template) RegisterPartialFile(filePath string, name string) error
- func (tpl *Template) RegisterPartialFiles(filePaths ...string) error
- func (tpl *Template) RegisterPartialTemplate(name string, template *Template)
- func (tpl *Template) RegisterPartials(partials map[string]string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Escape ¶
Escape escapes special HTML characters.
It can be used by helpers that return a SafeString and that need to escape some content by themselves.
Example ¶
tpl := MustParse("{{link url text}}") tpl.RegisterHelper("link", func(url string, text string) SafeString { return SafeString("<a href='" + Escape(url) + "'>" + Escape(text) + "</a>") }) ctx := map[string]string{ "url": "http://www.aymerick.com/", "text": "This is a <em>cool</em> website", } result := tpl.MustExec(ctx) fmt.Print(result)
Output: <a href='http://www.aymerick.com/'>This is a <em>cool</em> website</a>
func IsTrue ¶
func IsTrue(obj interface{}) bool
IsTrue returns true if obj is a truthy value.
Example ¶
output := "Empty array: " + Str(IsTrue([0]string{})) + "\n" output += "Non empty array: " + Str(IsTrue([1]string{"foo"})) + "\n" output += "Empty slice: " + Str(IsTrue([]string{})) + "\n" output += "Non empty slice: " + Str(IsTrue([]string{"foo"})) + "\n" output += "Empty map: " + Str(IsTrue(map[string]string{})) + "\n" output += "Non empty map: " + Str(IsTrue(map[string]string{"foo": "bar"})) + "\n" output += "Empty string: " + Str(IsTrue("")) + "\n" output += "Non empty string: " + Str(IsTrue("foo")) + "\n" output += "true bool: " + Str(IsTrue(true)) + "\n" output += "false bool: " + Str(IsTrue(false)) + "\n" output += "0 integer: " + Str(IsTrue(0)) + "\n" output += "positive integer: " + Str(IsTrue(10)) + "\n" output += "negative integer: " + Str(IsTrue(-10)) + "\n" output += "0 float: " + Str(IsTrue(0.0)) + "\n" output += "positive float: " + Str(IsTrue(10.0)) + "\n" output += "negative integer: " + Str(IsTrue(-10.0)) + "\n" output += "struct: " + Str(IsTrue(struct{}{})) + "\n" output += "nil: " + Str(IsTrue(nil)) + "\n" fmt.Println(output)
Output: Empty array: false Non empty array: true Empty slice: false Non empty slice: true Empty map: false Non empty map: true Empty string: false Non empty string: true true bool: true false bool: false 0 integer: false positive integer: true negative integer: true 0 float: false positive float: true negative integer: true struct: true nil: false
func MustRender ¶
MustRender parses a template and evaluates it with given context. It panics on error.
Note that this function call is not optimal as your template is parsed everytime you call it. You should use Parse() function instead.
Example ¶
tpl := "<h1>{{title}}</h1><p>{{body.content}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // render template with context output := MustRender(tpl, ctx) fmt.Print(output)
Output: <h1>foo</h1><p>bar</p>
func RegisterHelper ¶
func RegisterHelper(name string, helper interface{})
RegisterHelper registers a global helper. That helper will be available to all templates.
func RegisterHelpers ¶
func RegisterHelpers(helpers map[string]interface{})
RegisterHelpers registers several global helpers. Those helpers will be available to all templates.
func RegisterPartial ¶
RegisterPartial registers a global partial. That partial will be available to all templates.
func RegisterPartialTemplate ¶
RegisterPartialTemplate registers a global partial with given parsed template. That partial will be available to all templates.
func RegisterPartials ¶
RegisterPartials registers several global partials. Those partials will be available to all templates.
func RemoveAllPartials ¶
func RemoveAllPartials()
RemoveAllPartials removes all globally registered partials. This does not affect partials registered on a specific template.
func RemovePartial ¶
func RemovePartial(name string)
RemovePartial removes the partial registered under the given name. The partial will not be available globally anymore. This does not affect partials registered on a specific template.
func Render ¶
Render parses a template and evaluates it with given context
Note that this function call is not optimal as your template is parsed everytime you call it. You should use Parse() function instead.
Example ¶
tpl := "<h1>{{title}}</h1><p>{{body.content}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // render template with context output, err := Render(tpl, ctx) if err != nil { panic(err) } fmt.Print(output)
Output: <h1>foo</h1><p>bar</p>
func Str ¶
func Str(value interface{}) string
Str returns string representation of any basic type value.
Example ¶
output := Str(3) + " foos are " + Str(true) + " and " + Str(-1.25) + " bars are " + Str(false) + "\n" output += "But you know '" + Str(nil) + "' John Snow\n" output += "map: " + Str(map[string]string{"foo": "bar"}) + "\n" output += "array: " + Str([]interface{}{true, 10, "foo", 5, "bar"}) fmt.Println(output)
Output: 3 foos are true and -1.25 bars are false But you know '' John Snow map: map[foo:bar] array: true10foo5bar
Types ¶
type DataFrame ¶
type DataFrame struct {
// contains filtered or unexported fields
}
DataFrame represents a private data frame.
Cf. private variables documentation at: http://handlebarsjs.com/block_helpers.html
func NewDataFrame ¶
func NewDataFrame() *DataFrame
NewDataFrame instanciates a new private data frame.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options represents the options argument provided to helpers and context functions.
func (*Options) Ctx ¶
func (options *Options) Ctx() interface{}
Ctx returns current evaluation context.
func (*Options) NewDataFrame ¶
NewDataFrame instanciates a new data frame that is a copy of current evaluation data frame.
Parent of returned data frame is set to current evaluation data frame.
func (*Options) Params ¶
func (options *Options) Params() []interface{}
Params returns all parameters.
type SafeString ¶
type SafeString string
SafeString represents a string that must not be escaped.
A SafeString can be returned by helpers to disable escaping.
Example ¶
RegisterHelper("em", func() SafeString { return SafeString("<em>FOO BAR</em>") }) tpl := MustParse("{{em}}") result := tpl.MustExec(nil) fmt.Print(result)
Output: <em>FOO BAR</em>
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template represents a handlebars template.
func (*Template) Exec ¶
Exec evaluates template with given context.
Example ¶
source := "<h1>{{title}}</h1><p>{{body.content}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // evaluate template with context output, err := tpl.Exec(ctx) if err != nil { panic(err) } fmt.Print(output)
Output: <h1>foo</h1><p>bar</p>
func (*Template) ExecWith ¶
ExecWith evaluates template with given context and private data frame.
Example ¶
source := "<h1>{{title}}</h1><p>{{#body}}{{content}} and {{@baz.bat}}{{/body}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // computes private data frame frame := NewDataFrame() frame.Set("baz", map[string]string{"bat": "unicorns"}) // evaluate template output, err := tpl.ExecWith(ctx, frame) if err != nil { panic(err) } fmt.Print(output)
Output: <h1>foo</h1><p>bar and unicorns</p>
func (*Template) MustExec ¶
MustExec evaluates template with given context. It panics on error.
Example ¶
source := "<h1>{{title}}</h1><p>{{body.content}}</p>" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := MustParse(source) // evaluate template with context output := tpl.MustExec(ctx) fmt.Print(output)
Output: <h1>foo</h1><p>bar</p>
func (*Template) PrintAST ¶
PrintAST returns string representation of parsed template.
Example ¶
source := "<h1>{{title}}</h1><p>{{#body}}{{content}} and {{@baz.bat}}{{/body}}</p>" // parse template tpl := MustParse(source) // print AST output := tpl.PrintAST() fmt.Print(output)
Output: CONTENT[ '<h1>' ] {{ PATH:title [] }} CONTENT[ '</h1><p>' ] BLOCK: PATH:body [] PROGRAM: {{ PATH:content [] }} CONTENT[ ' and ' ] {{ @PATH:baz/bat [] }} CONTENT[ '</p>' ]
func (*Template) RegisterHelper ¶
RegisterHelper registers a helper for that template.
func (*Template) RegisterHelpers ¶
RegisterHelpers registers several helpers for that template.
func (*Template) RegisterPartial ¶
RegisterPartial registers a partial for that template.
func (*Template) RegisterPartialFile ¶
RegisterPartialFile reads given file and registers its content as a partial with given name.
func (*Template) RegisterPartialFiles ¶
RegisterPartialFiles reads several files and registers them as partials, the filename base is used as the partial name.
func (*Template) RegisterPartialTemplate ¶
RegisterPartialTemplate registers an already parsed partial for that template.
func (*Template) RegisterPartials ¶
RegisterPartials registers several partials for that template.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package ast provides structures to represent a handlebars Abstract Syntax Tree, and a Visitor interface to visit that tree.
|
Package ast provides structures to represent a handlebars Abstract Syntax Tree, and a Visitor interface to visit that tree. |
Package handlebars contains all the tests that come from handlebars.js project.
|
Package handlebars contains all the tests that come from handlebars.js project. |
Package lexer provides a handlebars tokenizer.
|
Package lexer provides a handlebars tokenizer. |
Package parser provides a handlebars syntax analyser.
|
Package parser provides a handlebars syntax analyser. |