A rule can take more than one action using a compound action.
Every
The most frequently used kind of compound action is the every
compound action. The every
compound action takes every action listed in order. The actions are listed inside curly braces with semicolon separators:
if x > 5 then every { notify("Hello World!", "This is my first message!"); notify("Hello Again!", "This is my second message!"); }
This would place two notification boxes on the page.
Because this is so common, the keyword every
is optional, and so this could be written as
if x > 5 then { notify("Hello World!", "This is my first message!"); notify("Hello Again!", "This is my second message!"); }
There is no limit to the number of actions that can be placed inside a compound action. While there is no linguistic restriction on mixing actions that send JavaScript and those that send directives, no current endpoint understands both directives and JavaScript, so that is not typically done.
Choose
The other kind of compound action is the choose
compound action. A choose
action takes a list of actions like every
, but instead of executing them all, it randomly picks one to execute. So, for example, the following would either send "This is one message!" or "This is another message!" randomly. The key reason for taking random action is to easily accommodate A/B behavior testing.
if x > 5 then choose { notify("Hello World!", "This is one message!"); notify("Hello World!", "This is another message!"); }
The choose
action can also be used to actually choose a specific action, like a case statement:
choose some_var { A => notify("Hello World!", "This is one message!"); B => notify("Hello World!", "This is another message!"); }
Notice that a variable, some_var
, has been inserted after choose
. Any valid KRL expression can follow choose
. The expression will be evaluated and its value will be compared to the action labels (A
or B
) in the action list, and the actions associated with labels that match exactly will be taken. Action labels are identifiers for specific actions. If the value does not match any label, then no action is taken--as if the action statement were missing.