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 #6 from mailgun/cclark/dev
Browse files Browse the repository at this point in the history
PIP:1374: Add ifLt helper
  • Loading branch information
Takumi2008 authored Aug 12, 2021
2 parents 55f5e37 + e640297 commit 7b331db
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
25 changes: 23 additions & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
RegisterHelper("lookup", lookupHelper)
RegisterHelper("equal", equalHelper)
RegisterHelper("ifGt", ifGtHelper)
RegisterHelper("ifLt", ifLtHelper)
}

// RegisterHelper registers a global helper. That helper will be available to all templates.
Expand Down Expand Up @@ -311,11 +312,11 @@ func ifGtHelper(a, b interface{}, options *Options) interface{} {

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

if aFloat > bFloat {
Expand All @@ -325,6 +326,26 @@ func ifGtHelper(a, b interface{}, options *Options) interface{} {
return options.Inverse()
}

func ifLtHelper(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
70 changes: 70 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,76 @@ var helperTests = []Test{
nil, nil, nil,
`foo is not greater than bar`,
},
{
"#ifGt helper non-numbers",
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifGt helper non-numbers with else condition",
`{{#ifGt foo bar}}foo is greater than bar{{else}}foo or bar are not numbers{{/ifGt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
`foo or bar are not numbers`,
},
{
"#ifLt helper with true literal",
`{{#ifLt foo 10}}foo is less than 10{{/ifLt}}`,
map[string]interface{}{"foo": 5},
nil, nil, nil,
`foo is less than 10`,
},
{
"#ifLt helper with false literal",
`{{#ifLt foo 10}}foo is less than 10{{/ifLt}}`,
map[string]interface{}{"foo": 15},
nil, nil, nil,
``,
},
{
"#ifLt helper with true literal from params",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with string comparison",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": "0", "bar": "5"},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with false literal from params",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 0, "bar": 5},
nil, nil, nil,
`foo is less than bar`,
},
{
"#ifLt helper with else condition",
`{{#ifLt foo bar}}foo is less than bar{{else}}foo is not less than bar{{/ifLt}}`,
map[string]interface{}{"foo": 6, "bar": 5},
nil, nil, nil,
`foo is not less than bar`,
},
{
"#ifLt helper non-numbers",
`{{#ifLt foo bar}}foo is less than bar{{/ifLt}}`,
map[string]interface{}{"foo": "foo", "bar": "bar"},
nil, nil, nil,
``,
},
{
"#ifLt helper non-numbers with else condition",
`{{#ifLt foo bar}}foo is greater than bar{{else}}foo or bar are not numbers{{/ifLt}}`,
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 7b331db

Please sign in to comment.