Plugin

EE 1
EE 2
Switchee

Developer

Mark Croxton

3rd Party (Free)

Download v2.0.6

Compatibility

  • LG Addon Updater
  • Multi Site Manager
  • Stand Alone Entry Form

Requirements

  • jQuery for the Control Panel

Tags

Use switch/case control logic in your templates.

With EEs if/else advanced conditionals, each condition is parsed before being removed at the end of the template 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.

Using Switchee instead you can ensure that unmatched conditions are removed from the template before they can be parsed. This can make your template significantly more efficient and your pages will render more quickly.

Switchee allows you to use regular expressions to perform advanced matching on case conditions. Like PHPs switch/case, Switchee supports a default value.

Example:

{exp:switchee variable = "{variable_to_test}" parse="inward"}
    
    {case value="value1"}
        Content to show
    {/case}
    
    {case value="value2" default="Yes"}
        Content to show
    {/case}
    
    {case value="value3"}
        Content to show
    {/case}
 
{!-- detect pagination, for example --}
 {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"}

Switchee v2.0.1 (beta) - now fully nestable - please help me test:
https://github.com/croxton/Switchee/blob/develop/pi.switchee.php

Download Switchee

EE Version Downloads Add-On Version Release Date
2.+ Download 2.0.6 2011-11-04 06:31 PM
1.x Download 1.6 2010-10-12 12:00 AM

Switchee Links

This entry was last updated January 27, 2012, 7:35 am.

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!

7 Reviews:

JCDerrick 12.14.11

JCDerrick

54321

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

54321

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

54321

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

54321

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

marcusneto 04.30.11

54321

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

Ryan Masuga 09.01.10

Ryan Masuga

54321

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

54321

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.