Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Merge pull request #7 from mailgun/cclark/dev
Browse files Browse the repository at this point in the history
PIP-1382: add ifEq helper
  • Loading branch information
Takumi2008 authored Aug 17, 2021
2 parents 7b331db + fb89138 commit 9830184
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
21 changes: 21 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
RegisterHelper("equal", equalHelper)
RegisterHelper("ifGt", ifGtHelper)
RegisterHelper("ifLt", ifLtHelper)
RegisterHelper("ifEq", ifEqHelper)
}

// RegisterHelper registers a global helper. That helper will be available to all templates.
Expand Down Expand Up @@ -346,6 +347,26 @@ func ifLtHelper(a, b interface{}, options *Options) interface{} {
return options.Inverse()
}

func ifEqHelper(a, b interface{}, options *Options) interface{} {
var aFloat, bFloat float64
var err error

if aFloat, err = floatValue(a); err != nil {
// TODO: Log conversion failure.
return options.Inverse()
}
if bFloat, err = floatValue(b); err != nil {
// TODO: Log conversion failure
return options.Inverse()
}

if aFloat == bFloat {
return options.Fn()
}
// Evaluate possible else condition.
return options.Inverse()
}

// #unless block helper
func unlessHelper(conditional interface{}, options *Options) interface{} {
if options.isIncludableZero() || IsTrue(conditional) {
Expand Down
56 changes: 56 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,62 @@ var helperTests = []Test{
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#ifEq helper with true literal",
`{{#ifEq foo 10}}foo is equal to 10{{/ifEq}}`,
map[string]interface{}{"foo": 10},
nil, nil, nil,
`foo is equal to 10`,
},
{
"#ifEq helper with false literal",
`{{#ifEq foo 10}}foo is equal to 10{{/ifEq}}`,
map[string]interface{}{"foo": 15},
nil, nil, nil,
``,
},
{
"#ifEq helper with true literal from params",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 5, "bar": 5},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with string comparison",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": "5", "bar": "5"},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with false literal from params",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 5, "bar": 5},
nil, nil, nil,
`foo is equal to bar`,
},
{
"#ifEq helper with else condition",
`{{#ifEq foo bar}}foo is equal to bar{{else}}foo is not equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": 6, "bar": 5},
nil, nil, nil,
`foo is not equal to bar`,
},
{
"#ifEq helper non-numbers",
`{{#ifEq foo bar}}foo is equal to bar{{/ifEq}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifEq helper non-numbers with else condition",
`{{#ifEq foo bar}}foo is equal to bar{{else}}foo or bar are not numbers{{/ifEq}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#equal helper inside HTML tag",
`<option value="test" {{#equal value "test"}}selected{{/equal}}>Test</option>`,
Expand Down

0 comments on commit 9830184

Please sign in to comment.