Conditional Branching
Useful when there are multiple choices
Preface
Branching using if
action's pipeline and comparison operators - these operators don't need to be inside if
branch. if
statements always need to have an enclosing end
.
Function
Case
Example
if
{{if (condition)}} output {{end}}
Initialization statement can also be inside if
statement with conditional statement, limiting the initialized scope to that if
statement.
{{$x := 24}}
{{if eq ($x := 42) 42}} Inside: {{$x}} {{end}}
Outside: {{$x}}
else if
{{if (condition)}} output1 {{else if (condition)}} output2 {{end}}
You can have as manyelse if
statements as many different conditionals you have.
else
{{if (condition)}} output1 {{else}} output2 {{end}}
not
{{if not (condition)}} output {{end}}
and
{{if and (cond1) (cond2) (cond3)}} output {{end}}
or
{{if or (cond1) (cond2) (cond3)}} output {{end}}
Equal: eq
{{if eq .Channel.ID ########}} output {{end}}
Not equal: ne
{{$x := 7}} {{$y := 8}} {{ne $x $y}}
returns true
Less than: lt
{{if lt (len .Args) 5}} output {{end}}
Less than or equal: le
{{$x := 7}} {{$y := 8}} {{le $x $y}}
returns true
Greater than: gt
{{if gt (len .Args) 1}} output {{end}}
Greater than or equal: ge
{{$x := 7}} {{$y := 8}} {{ge $x $y}}
returns false
When you use if-else
functions, it will only execute the first one that matches the condition you want even if it matches multiple conditions. Example here.
Example:
It will only execute the first one that matches the condition you want.
{{$x := 13}}
{{if ne $x 1}}
Will execute this one.
{{else if eq $x 13}}
Will not execute this one.
{{else}}
Will not execute this one.{{end}}
If you write the codes like this, it will execute all parts.
{{$x := 13}}
{{if ne $x 1}}
Will execute this one.{{end}}
{{if eq $x 13}}
Will execute this one.{{end}}
{{if gt $x 1}}
Will execute this one.{{end}}
You can also define the variable like the following one.
{{if ge ($x := randInt 10) 4}}
{{$x}} is greater than 4
{{else}}
{{$x}} is smaller than 4
{{end}}
Last updated
Was this helpful?