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 #3 from mailgun/cclark/dev
Browse files Browse the repository at this point in the history
PIP-1372: Add ifGt helper
  • Loading branch information
Takumi2008 authored Aug 10, 2021
2 parents 100e261 + c13c01e commit 435acb9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func init() {
RegisterHelper("log", logHelper)
RegisterHelper("lookup", lookupHelper)
RegisterHelper("equal", equalHelper)
RegisterHelper("ifGt", ifGtHelper)
}

// RegisterHelper registers a global helper. That helper will be available to all templates.
Expand Down Expand Up @@ -302,6 +303,23 @@ func ifHelper(conditional interface{}, options *Options) interface{} {
return options.Inverse()
}

func ifGtHelper(a, b interface{}, options *Options) interface{} {
// Non-integer comparisons are not supported by the ifGt helper. Return empty string.
var aInt, bInt int
var ok bool
if aInt, ok = a.(int); !ok {
return ""
}
if bInt, ok = b.(int); !ok {
return ""
}
if aInt > bInt {
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
42 changes: 42 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,48 @@ var helperTests = []Test{
nil, nil, nil,
``,
},
{
"#ifGt helper with true literal",
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": 11},
nil, nil, nil,
`foo is greater than 10`,
},
{
"#ifGt helper with false literal",
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": 5},
nil, nil, nil,
``,
},
{
"#ifGt helper with true literal from params",
`{{#ifGt foo bar}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": 5, "bar": 0},
nil, nil, nil,
`foo is greater than 10`,
},
{
"#ifGt helper with string comparison, is improper imput should return empty string",
`{{#ifGt foo bar}}foo is greater than 10{{/ifGt}}`,
map[string]interface{}{"foo": "5", "bar": "0"},
nil, nil, nil,
``,
},
{
"#ifGt helper with false literal from params",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": 5, "bar": 0},
nil, nil, nil,
`foo is greater than bar`,
},
{
"#ifGt helper with else condition",
`{{#ifGt foo bar}}foo is greater than bar{{else}}foo is not greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is not greater than bar`,
},
{
"#equal helper inside HTML tag",
`<option value="test" {{#equal value "test"}}selected{{/equal}}>Test</option>`,
Expand Down

0 comments on commit 435acb9

Please sign in to comment.