Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { source := `Hello {{name}}` data := map[string]string{ "name": "World", } tpl, err := mario.New().Parse(source) if err != nil { panic(err) } var b strings.Builder if err := tpl.Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) }
Output: Hello World
Example (Html_escape) ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { source := "{{text}}\n{{{text}}}" data := map[string]string{ "text": "This is <html> Tags", } var b strings.Builder if err := mario.Must(mario.New().Parse(source)).Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) }
Output: This is <html> Tags This is <html> Tags
Example (Map) ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { source := `{ "name": "{{name}}", "age": {{age}} }` data := map[string]interface{}{ "name": "Mario", "age": 12, } tpl, err := mario.New().Parse(source) if err != nil { panic(err) } var b strings.Builder if err := tpl.Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) }
Output: { "name": "Mario", "age": 12 }
Example (Rendering_context) ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { source := `<div class="post"> <h1>By {{author.firstName}} {{author.lastName}}</h1> <div class="body">{{body}}</div> <h1>Comments</h1> {{#each comments}} <h2>By {{author.firstName}} {{author.lastName}}</h2> <div class="body">{{content}}</div> {{/each}} </div>` data := Post{ Person{"Jean", "Valjean"}, "Life is difficult", []Comment{ Comment{ Person{"Marcel", "Beliveau"}, "LOL!", }, }, } var b strings.Builder if err := mario.Must(mario.New().Parse(source)).Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) } 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 }
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 ¶
- func Escape(s string) string
- func IsTrue(obj interface{}) bool
- func RegisterHelper(name string, fn interface{})
- func ResetHelpers()
- func Str(value interface{}) string
- type DataFrame
- type Helper
- 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 SafeString
- type Template
- func (tpl *Template) Execute(w io.Writer, ctx interface{}) error
- func (tpl *Template) ExecuteWith(w io.Writer, ctx interface{}, frame *DataFrame) (err error)
- func (tpl *Template) Parse(source string) (*Template, error)
- func (tpl *Template) Program() *ast.Program
- func (tpl *Template) WithHelper(name string, helper *Helper) *Template
- func (tpl *Template) WithHelperFunc(name string, fn interface{}) *Template
- func (tpl *Template) WithPartial(name string, template *Template) *Template
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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.
func RegisterHelper ¶
func RegisterHelper(name string, fn interface{})
RegisterHelper to register new build-in helpers
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 Helper ¶
Helper implement functionality that is not part of the Handlebars language itself https://handlebarsjs.com/guide/expressions.html#helpers
Example (Safe_string) ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { tpl, err := mario.New(). WithHelperFunc("link", func(url, text string) mario.SafeString { return mario.SafeString("<a href='" + mario.Escape(url) + "'>" + mario.Escape(text) + "</a>") }). Parse("{{link url text}}") if err != nil { panic(err) } data := map[string]string{ "url": "http://www.aymerick.com/", "text": "This is a <em>cool</em> website", } var b strings.Builder if err := tpl.Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) }
Output: <a href='http://www.aymerick.com/'>This is a <em>cool</em> website</a>
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.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template represents a handlebars template.
func Must ¶
Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
Example ¶
package main import ( "fmt" "strings" "github.com/imantung/mario" ) func main() { source := `Hello {{name}}` data := map[string]string{ "name": "World", } var b strings.Builder if err := mario.Must(mario.New().Parse(source)).Execute(&b, data); err != nil { panic(err) } fmt.Println(b.String()) }
Output: Hello World
func (*Template) ExecuteWith ¶
ExecuteWith evaluates template with given context and private data frame.
func (*Template) WithHelper ¶
WithHelper to set helper
func (*Template) WithHelperFunc ¶
WithHelperFunc to create and set helper
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. |