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

Commit c13c01e

Browse files
committed
add ifGt helper
1 parent 100e261 commit c13c01e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

helper.go

+18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func init() {
3232
RegisterHelper("log", logHelper)
3333
RegisterHelper("lookup", lookupHelper)
3434
RegisterHelper("equal", equalHelper)
35+
RegisterHelper("ifGt", ifGtHelper)
3536
}
3637

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

306+
func ifGtHelper(a, b interface{}, options *Options) interface{} {
307+
// Non-integer comparisons are not supported by the ifGt helper. Return empty string.
308+
var aInt, bInt int
309+
var ok bool
310+
if aInt, ok = a.(int); !ok {
311+
return ""
312+
}
313+
if bInt, ok = b.(int); !ok {
314+
return ""
315+
}
316+
if aInt > bInt {
317+
return options.Fn()
318+
}
319+
// Evaluate possible else condition.
320+
return options.Inverse()
321+
}
322+
305323
// #unless block helper
306324
func unlessHelper(conditional interface{}, options *Options) interface{} {
307325
if options.isIncludableZero() || IsTrue(conditional) {

helper_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,48 @@ var helperTests = []Test{
194194
nil, nil, nil,
195195
``,
196196
},
197+
{
198+
"#ifGt helper with true literal",
199+
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
200+
map[string]interface{}{"foo": 11},
201+
nil, nil, nil,
202+
`foo is greater than 10`,
203+
},
204+
{
205+
"#ifGt helper with false literal",
206+
`{{#ifGt foo 10}}foo is greater than 10{{/ifGt}}`,
207+
map[string]interface{}{"foo": 5},
208+
nil, nil, nil,
209+
``,
210+
},
211+
{
212+
"#ifGt helper with true literal from params",
213+
`{{#ifGt foo bar}}foo is greater than 10{{/ifGt}}`,
214+
map[string]interface{}{"foo": 5, "bar": 0},
215+
nil, nil, nil,
216+
`foo is greater than 10`,
217+
},
218+
{
219+
"#ifGt helper with string comparison, is improper imput should return empty string",
220+
`{{#ifGt foo bar}}foo is greater than 10{{/ifGt}}`,
221+
map[string]interface{}{"foo": "5", "bar": "0"},
222+
nil, nil, nil,
223+
``,
224+
},
225+
{
226+
"#ifGt helper with false literal from params",
227+
`{{#ifGt foo bar}}foo is greater than bar{{/ifGt}}`,
228+
map[string]interface{}{"foo": 5, "bar": 0},
229+
nil, nil, nil,
230+
`foo is greater than bar`,
231+
},
232+
{
233+
"#ifGt helper with else condition",
234+
`{{#ifGt foo bar}}foo is greater than bar{{else}}foo is not greater than bar{{/ifGt}}`,
235+
map[string]interface{}{"foo": 0, "bar": 5},
236+
nil, nil, nil,
237+
`foo is not greater than bar`,
238+
},
197239
{
198240
"#equal helper inside HTML tag",
199241
`<option value="test" {{#equal value "test"}}selected{{/equal}}>Test</option>`,

0 commit comments

Comments
 (0)