mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-21 00:02:45 +00:00
parent
202510fdc9
commit
0462c96a5a
3 changed files with 25 additions and 0 deletions
|
@ -142,6 +142,15 @@ func (n *Namespace) Lt(a, b interface{}) bool {
|
||||||
return left < right
|
return left < right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Conditional can be used as a ternary operator.
|
||||||
|
// It returns a if condition, else b.
|
||||||
|
func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} {
|
||||||
|
if condition {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (*Namespace) compareGetFloat(a interface{}, b interface{}) (float64, float64) {
|
func (*Namespace) compareGetFloat(a interface{}, b interface{}) (float64, float64) {
|
||||||
var left, right float64
|
var left, right float64
|
||||||
var leftStr, rightStr *string
|
var leftStr, rightStr *string
|
||||||
|
|
|
@ -221,3 +221,12 @@ func TestTimeUnix(t *testing.T) {
|
||||||
toTimeUnix(iv)
|
toTimeUnix(iv)
|
||||||
}(t)
|
}(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConditional(t *testing.T) {
|
||||||
|
assert := require.New(t)
|
||||||
|
n := New()
|
||||||
|
a, b := "a", "b"
|
||||||
|
|
||||||
|
assert.Equal(a, n.Conditional(true, a, b))
|
||||||
|
assert.Equal(b, n.Conditional(false, a, b))
|
||||||
|
}
|
||||||
|
|
|
@ -69,6 +69,13 @@ func init() {
|
||||||
[][2]string{},
|
[][2]string{},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.Conditional,
|
||||||
|
[]string{"cond"},
|
||||||
|
[][2]string{
|
||||||
|
{`{{ cond (eq (add 2 2) 4) "2+2 is 4" "what?" | safeHTML }}`, `2+2 is 4`},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
return ns
|
return ns
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue