Switchee

Developer

Mark Croxton

3rd Party (Free)

1367 downloads (last 90 days)


EE Version Support

  • ExpressionEngine 1.x
  • ExpressionEngine 2
  • ExpressionEngine 3
  • ExpressionEngine 4

Compatibility

If an item is crossed out, it might be untested, not applicable or incompatible. Contact the developer to be sure.

  • Updater
  • Multi Site Manager
  • Stand Alone Entry Form
  • Low Variables
  • Content Elements
  • Better Workflow
  • Matrix
  • Grid
  • Webservice
  • Publisher

Requirements

  • jQuery for the Control Panel

Add-On Type(s)

Plugin

Tags

Editor's Pick
Each month the devot:ee staff picks an add-on that's especially well thought out, well built, and useful. They're add-ons that we love and think you will too!

Switchee was chosen in May 2012.

Use switch/case control logic in your templates.

Switchee adds a powerful switch/case control structure to ExpressionEngine templates, and can be used as an alternative to if/else conditionals.

Switchee vs If/Else

Prior to EE.2.9.x, if/else “advanced conditionals” were parsed at the very end of the parsing process, with the result that tags within non-matching conditons were still parsed unecessarily before being removed.

With the release of EE 2.9.0, if/else conditionals can now evaluate regular expressions and are parsed “when ready” - when the variable being evaluated has a known value. In _most_ cases this solves the main problem that Switchee was designed to solve, however there are some occassions where this can still result in non-matching conditions being parsed. Unlike if/else, Switchee is parsed linearly with other tags - immediately after preceding tags but before any following tags on a given layer of the template - so you can rely on it to always remove non-matching conditions before they can be parsed.

Switchee has the following advantages over if/else:

  • Parsed linearly
  • Evaluate global, GET, POST and Stash variables
  • Optionally, all matching cases can be returned with `match=“all”`
  • Fast and stable with very large strings without hitting PCRE memory / recursion issues (tested with millions of lines)

If/Else has the following advantages over Switchee:

  • Native, first-party functionality
  • Comparison, logical and mathematical operators
  • String concatenation
  • Error handling

So which should you use today?

Presentation logic

For general presentation logic if/else is less verbose and the operators make it the more flexible choice.

Dynamic variables

When working with dynamic variables such as those created by Stash or passed in the POST array, Switchee is the obvious choice.

Routing logic

Need to output an entirely different page for a particular segment value? I strongly recommend you use Resource Router or the native template routes functionality instead of complicating your templates with routing logic.

Installation

Unzip the download and rename the extracted directory to ‘switchee’.

ExpressionEngine 2.x

Move the ‘switchee’ folder to ‘./system/expressionengine/third_party’.

ExpressionEngine 3.x+

Move the ‘switchee’ folder to ‘./system/user/addons’.

How to use

{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:
			{ = {
			| = |
			} = }
			Use '' to represent an empty string
		{/case}
		
		{case value="value4" default="yes"}	
			
			{!-- you can also nest Switchee by leaving off the 'exp:' in nested tags : --}
			{switchee variable="{another_variable_to_test}" parse="inward"}
				{case value="value1"}
					nested content to show
				{/case}
			{/switchee}	
			
		{/case}
		
	{/exp:switchee}

Support for no_result blocks inside wrapped tags:

{if switchee_no_results}
		{redirect="channel/noresult"}
	{/if}

Return all matching cases with `match=“all”`

{!-- Output: 'Orange O' --}	
	{exp:switchee variable="orange" match="all" parse="inward"}
		
		{case value="orange"}
			Orange
		{/case}
		
		{case value="#^o#"}
			O
		{/case}
		
		{case value="apple"}
			Apple
		{/case}
		
	{/exp:switchee}

GET and POST globals can be evaluated by prefixing with get: or post:, e.g.:

{exp:switchee variable = "post:my_var" parse="inward"}

Global variable can be evaluated by prefixing with global:, e.g.:

{exp:switchee variable = "global:my_var" parse="inward"}

Stash variables can be evaluated by prefixing with stash:, e.g.:

{exp:switchee variable = "stash:my_var" parse="inward"}

Download Switchee

EE Support Downloads Add-On Version Release Date
Not Specified Download 1.6 Oct 12, 2010
2.2.0+ Download 3.0.1 Sep 4, 2018

Switchee Links

This entry was created February 16, 2010, 6:24 am.
This entry was last updated September 4, 2018, 12:05 pm.

Disclaimer: Information about ExpressionEngine add-ons is provided as a service to you, the user, and every member of the ExpressionEngine community. devot:ee is not responsible if you hose, mangle, wreck, or otherwise destroy your EE website by installing an add-on that you found out about at this site, regardless of its rating, Favorites status, commercial or free status, or general popularity. Caveat EEmptor!

Returns: devot:ee has a 30-day return policy on all commercial add-ons sold through devot-ee.com. If you need to return an add-on, do not go to the developer or the developer's site, but rather visit our returns page at https://devot-ee.com/returns to initiate your return. If you have questions, email support@devot-ee.com.

14 Reviews:

stefanos 01.23.18

stefanos
Rating - {addon_rating_average}

A very powerful control logic add-on, easy to use!
Fantastic job Marc!! And it’s EE4 compatible !

Loughlin 05.27.14

Loughlin
Rating - {addon_rating_average}

This is one of those plugins that makes you wonder when it will be absorbed into the core of Expression Engine.  This never lets me down, this gets implemented along with stash every single install…

lehrerfreund 04.30.14

lehrerfreund
Rating - {addon_rating_average}

The native if-else-logic of EE is very performance-intensive. Switchee is SO ultra-useful, because it allows you to use complex templates without bringing the server onto his knees.

Interact Marketing 02.11.14

Interact Marketing
Rating - {addon_rating_average}

Should be integrated into EE by default

96black 11.19.13

96black
Rating - {addon_rating_average}

A tried and true plugin that we use everywhere often to get pagination and article listing vs. detail pages showing. All with one simple Switchee tag pair!

Ben Kohl 08.09.13

Ben Kohl
Rating - {addon_rating_average}

I can’t imagine building an EE site without this add-on. The nesting capability is outstanding.

Media Surgery 03.16.12

Media Surgery
Rating - {addon_rating_average}

Excellent addition to the EE ‘performance’ range of add ons.

JCDerrick 12.14.11

JCDerrick
Rating - {addon_rating_average}

This is a brilliant add-on for replacing EE’s native conditional statements. At first I wasn’t sure how much of a performance boost I’d really see using this add-on, but as time has gone on, I’ve realized this is an invaluable tool for any EE developer. Excellent add-on.

Curtis Blackwell 08.25.11

Curtis Blackwell
Rating - {addon_rating_average}

Being able to nest Switchee with the v2.0.1 beta has greatly simplified quite a few of my templates and significantly reduced my number of templates.

sm9 07.15.11

sm9
Rating - {addon_rating_average}

Switchee gave me a nice boost in performance over using standard EE conditionals. It even helped on a page full of basic conditionals, reducing page load times by 80% in some cases. Fantastic addon, thanks!

iain 07.14.11

iain
Rating - {addon_rating_average}

I couldn’t believe the performance gains when I first saw them using this add-on. Game changer.

marcusneto 04.30.11

marcusneto
Rating - {addon_rating_average}

Amazing. I love the functionality that this adds to our arsenal. Great work!

Ryan Masuga 09.01.10

Ryan Masuga
Rating - {addon_rating_average}

I frequently use a single template to do various things (list of entries, single-entry page and more) and Switchee allows me to do this knowing that we won’t have a lot of unnecessary queries running for non-matching cases.

Much better and cleaner than using simple conditionals. Nice work.

Jacob Russell 09.01.10

Jacob Russell
Rating - {addon_rating_average}

Great way to get around the problems with EE complex if statements parsing their contents early, which can put a major load on pages.  Also useful for situations where Case makes more sense then If/Else, obviously.