Skip to content

Commit

Permalink
adds doc for the handlebars struct tag
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed Dec 9, 2016
1 parent ff5da52 commit 534a47e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ result := tpl.MustExec(ctx)

The rendering context can contain any type of values, including `array`, `slice`, `map`, `struct` and `func`.

When using structs, be warned that only exported fields are accessible. However you can access exported fields in template with their lowercase names.
When using structs, be warned that only exported fields are accessible. However you can access exported fields in template with their lowercase names. For example, both `{{author.firstName}}` and `{{Author.FirstName}}` references give the same result, as long as `Author` and `FirstName` are exported struct fields.

For example, both `{{author.firstName}}` and `{{Author.FirstName}}` references give the same result, as long as `Author` and `FirstName` are exported struct fields.
More, you can use the `handlebars` struct tag to specify a template variable name different from the struct field name.

```go
package main
Expand All @@ -211,7 +211,7 @@ func main() {
{{#each comments}}
<h2>By {{author.firstName}} {{author.lastName}}</h2>
<div class="body">{{body}}</div>
<div class="body">{{content}}</div>
{{/each}}
</div>`

Expand All @@ -222,7 +222,7 @@ func main() {

type Comment struct {
Author Person
Body string
Body string `handlebars:"content"`
}

type Post struct {
Expand Down Expand Up @@ -262,7 +262,6 @@ Output:
</div>
```


## HTML Escaping

By default, the result of a mustache expression is HTML escaped. Use the triple mustache `{{{` to output unescaped values.
Expand Down
4 changes: 2 additions & 2 deletions raymond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Example_struct() {
{{#each comments}}
<h2>By {{fullName author}}</h2>
<div class="body">{{body}}</div>
<div class="body">{{content}}</div>
{{/each}}
</div>`

Expand All @@ -43,7 +43,7 @@ func Example_struct() {

type Comment struct {
Author Person
Body string
Body string `handlebars:"content"`
}

type Post struct {
Expand Down

0 comments on commit 534a47e

Please sign in to comment.