A very simple plugin that allows you to create switch/case control logic in your templates.
With EEs if/else conditionals, each condition is parsed before being removed at the end of the parsing process. This means if you wrap if/else tags around lots of other tags then your template will be running many unnecessary queries and functions.
As Switchee is a tag we can use parse=“inward” to ensure that unmatched conditions are not parsed before being removed from the template.
Switchee can only be used for simple conditional logic. Like PHPs switch/case, it supports a default value.
Example:
{exp:switchee variable = "{variable_to_test}" parse="inward"}
{case value="value1|value2"}
Content to show
{/case}
{case value="value3" default="Yes"}
Content to show
{/case}
{case value="#^P(\d+)$#|''"}
Use regular expressions enclosed by hashes #regex#
Be careful to encode the following reserved characters as follows:
{ = & #123;
| = & #124;
} = & #125;
Use '' to represent an empty string
{/case}
{/exp:switchee}
Note that ‘default’ can also be used on it’s own:
{case default="Yes"}
In some of my templates I’ve seen a 30% decrease in queries by using Switchee in preference to if/else.
17 Feb 2010 - updated to version 1.1
* Now allows multiple case values separated by pipe ‘|’.
19 Feb 2010 - updated to version 1.2
* Now supports regular expression matching like so: #regex#
* Supports empty string matches represesnted by ‘’ or “”
22 Feb 2010 - updated to version 1.3
* Added support for reserved characters in regular expressions
* Multiple regular expressions separated by | can now be used safely for one case value
24 Feb 2010 - updated to version 1.4
* fixed a minor bug
Still to do: Enable Switchee to be nested inside itself.
