Skip to content

Commit

Permalink
adds the #equal helper (fixes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed Dec 9, 2016
1 parent 534a47e commit 72acac2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The full API documentation is available here: <http://godoc.org/github.com/aymer
- [The `with` block helper](#the-with-block-helper)
- [The `lookup` helper](#the-lookup-helper)
- [The `log` helper](#the-log-helper)
- [The `equal` helper](#the-equal-helper)
- [Block Helpers](#block-helpers)
- [Block Evaluation](#block-evaluation)
- [Conditional](#conditional)
Expand Down Expand Up @@ -642,6 +643,41 @@ The `log` helper allows for logging while rendering a template.
Note that the handlebars.js `@level` variable is not supported.


#### The `equal` helper

The `equal` helper renders a block if the string version of both arguments are equals.

For example that template:

```html
{{#equal foo "bar"}}foo is bar{{/equal}}
{{#equal foo baz}}foo is the same as baz{{/equal}}
{{#equal nb 0}}nothing{{/equal}}
{{#equal nb 1}}there is one{{/equal}}
{{#equal nb "1"}}everything is stringified before comparison{{/equal}}
```

With that context:

```go
ctx := map[string]interface{}{
"foo": "bar",
"baz": "bar",
"nb": 1,
}
```

Outputs:

```html
foo is bar
foo is the same as baz

there is one
everything is stringified before comparison
```


### Block Helpers

Block helpers make it possible to define custom iterators and other functionality that can invoke the passed block with a new context.
Expand Down
11 changes: 11 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
RegisterHelper("each", eachHelper)
RegisterHelper("log", logHelper)
RegisterHelper("lookup", lookupHelper)
RegisterHelper("equal", equalHelper)
}

// RegisterHelper registers a global helper. That helper will be available to all templates.
Expand Down Expand Up @@ -369,3 +370,13 @@ func logHelper(message string) interface{} {
func lookupHelper(obj interface{}, field string, options *Options) interface{} {
return Str(options.Eval(obj, field))
}

// #equal helper
// Ref: https://github.com/aymerick/raymond/issues/7
func equalHelper(a interface{}, b interface{}, options *Options) interface{} {
if Str(a) == Str(b) {
return options.Fn()
}

return ""
}
68 changes: 68 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,74 @@ var helperTests = []Test{
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with same string var",
`{{#equal foo "bar"}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different string var",
`{{#equal foo "baz"}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "bar"},
nil, nil, nil,
``,
},
{
"#equal helper with same string vars",
`{{#equal foo bar}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "baz", "bar": "baz"},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different string vars",
`{{#equal foo bar}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": "baz", "bar": "tag"},
nil, nil, nil,
``,
},
{
"#equal helper with same integer var",
`{{#equal foo 1}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": 1},
nil, nil, nil,
`YES MAN`,
},
{
"#equal helper with different integer var",
`{{#equal foo 0}}YES MAN{{/equal}}`,
map[string]interface{}{"foo": 1},
nil, nil, nil,
``,
},
{
"#equal helper inside HTML tag",
`<option value="test" {{#equal value "test"}}selected{{/equal}}>Test</option>`,
map[string]interface{}{"value": "test"},
nil, nil, nil,
`<option value="test" selected>Test</option>`,
},
{
"#equal full example",
`{{#equal foo "bar"}}foo is bar{{/equal}}
{{#equal foo baz}}foo is the same as baz{{/equal}}
{{#equal nb 0}}nothing{{/equal}}
{{#equal nb 1}}there is one{{/equal}}
{{#equal nb "1"}}everything is stringified before comparison{{/equal}}`,
map[string]interface{}{
"foo": "bar",
"baz": "bar",
"nb": 1,
},
nil, nil, nil,
`foo is bar
foo is the same as baz
there is one
everything is stringified before comparison`,
},
}

//
Expand Down

0 comments on commit 72acac2

Please sign in to comment.