{switch:condition:case1:result1:...:default}

Description

Tests a condition value against a list of cases and returns the result of the first matching case. Each case is a regular expression matched against the condition with preg_match, so a case succeeds when its pattern is found anywhere in the condition - anchor with ^ and $ for an exact match. Cases and results alternate after the condition. The first matching case wins and its result is returned; if no case matches, the optional last parameter (an unpaired trailing value) is returned as the default. An empty case matches any condition, so it works as a catch-all else branch. In the chosen result, _#1 is replaced by the condition value. The colon separates parameters, so to keep a literal colon inside a case or result escape it as #: (the same trick that protects a URL as http#://). A condition that itself contains colons can use the parentheses form switch(condition) - the first close-parenthesis ends the condition - after which the cases and results follow as usual. switch is never cached: it is re-evaluated on every request.

Parameters

condition required

The value to test, usually a field-getter or alias whose expanded value is matched against each case. Compared with preg_match, so the cases are treated as regular expressions.

Allowed values The value to match against each case. Tested using preg_match — so any valid PHP regex can be used as a case pattern. Required.
case1...caseN optional

A regular expression matched against the condition with preg_match. It matches when the pattern is found anywhere in the condition; anchor with ^ and $ for an exact match. An empty case matches any condition (catch-all). Cases alternate with results and may repeat.

Allowed values Pattern matched against condition using preg_match. An empty case ("") matches any condition (catch-all). Cases are evaluated left-to-right; the first match wins. Required (at least one).
result1...resultN optional

The output returned when its preceding case matches. _#1 inside the result is replaced by the condition value. To include a literal colon, escape it as #:.

Allowed values Text to return when the corresponding case matches. is replaced with the condition value. Required per case.
default optional

Optional final value, given as an unpaired trailing parameter. Returned when no case matches. If every parameter after the condition is paired, there is no default and a non-match returns an empty string.

Allowed values Returned when no case matches. This is the last parameter when there is an odd total number of parameters. Optional — if omitted and no case matches, returns empty string.

Examples

test{switch:event:news:News:event:Event:Other}
ExpectedEvent
ActualEvent
Tests the condition against each case in turn. Here news does not match the condition event, event does, so its result Event is returned.
test{switch:user123:^admin:Admin:^user:User:Guest}
ExpectedUser
ActualUser
Each case is a regex matched with preg_match. ^user matches user123 at the start, so User wins. Anchor with ^ and $ for an exact match; without anchors the pattern matches anywhere in the condition.
test{switch:blog:news:News:event:Event:Other}
ExpectedOther
ActualOther
When no case matches, the last unpaired parameter is the default. blog matches neither news nor event, so Other is returned.
test{switch:anything:no:N::FALLBACK}
ExpectedFALLBACK
ActualFALLBACK
An empty case matches any condition. Here no does not match, then the empty case (the :: pair) matches and returns FALLBACK. This is an alternative to a trailing default.
test{switch:open:open:Status#: _#1:none}
ExpectedStatus: open
ActualStatus: open
The colon separates parameters, so a bare colon in a result would split it. Escape it as #: to keep a literal colon. Here the result Status#: _#1 yields Status: open. Without the escape the result would be truncated at the colon.
test{switch(10:30)^1:morning:afternoon}
Expectedmorning
Actualmorning
The parentheses form lets the condition contain colons; the first close-parenthesis ends it. Here the condition 10:30 starts with 1 (regex ^1), so morning is returned. The same value would be impossible with the colon form because 10:30 would split into two parameters.
test{switch:news:news:Section is _#1:Other}
ExpectedSection is news
ActualSection is news
In the chosen result, _#1 is replaced by the condition value. The case news matches, so Section is _#1 becomes Section is news.
vr2999