package handlebars_test import ( "fmt" "git.reinaldyrafli.com/aldy505/handlebars-go" ) func Example() { source := "

{{title}}

{{body.content}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // parse template tpl := handlebars.MustParse(source) // evaluate template with context output := tpl.MustExec(ctx) // alternatively, for one shots: // output := MustRender(source, ctx) fmt.Print(output) // Output:

foo

bar

} func Example_struct() { source := `

By {{fullName author}}

{{body}}

Comments

{{#each comments}}

By {{fullName author}}

{{content}}
{{/each}}
` 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{ { Person{"Marcel", "Beliveau"}, "LOL!", }, }, } handlebars.RegisterHelper("fullName", func(person Person) string { return person.FirstName + " " + person.LastName }) output := handlebars.MustRender(source, ctx) fmt.Print(output) // Output:
//

By Jean Valjean

//
Life is difficult
// //

Comments

// //

By Marcel Beliveau

//
LOL!
//
} func ExampleRender() { tpl := "

{{title}}

{{body.content}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // render template with context output, err := handlebars.Render(tpl, ctx) if err != nil { panic(err) } fmt.Print(output) // Output:

foo

bar

} func ExampleMustRender() { tpl := "

{{title}}

{{body.content}}

" ctx := map[string]interface{}{ "title": "foo", "body": map[string]string{"content": "bar"}, } // render template with context output := handlebars.MustRender(tpl, ctx) fmt.Print(output) // Output:

foo

bar

}