Monday, October 6, 2008

Switch Statment for Smarty

There is a complete and working switch plugin. It works exactly as a php switch and you can even use variables and modifiers for the conditions. Feel free to use it and let me know if you have and problems.

11/23/2010
I moved the code to github for matinance. https://github.com/pynej/Smarty-Switch-Statement/archives/Smarty-2.0

03/08/2008 - Updated to version 2
This update changes the break attribute to work how you would expect. That is, setting the break attribute will cause smarty to automatically render a {break} tag before it starts the NEXT case or default section. I strongly recommend updating to this version of the switch plugin, as the older version was very counter intuitive in this aspect.

02/09/2010 - Posted Smarty 3 Version
A separate version of this plug-in is available for Smarty 3 here.

(Also, this does work with nested switches, witch is why you see it accessing the _switchData variable. Be sure to not alter this variable in smarty code, as it will make the switches behave incorrectly.)

Both of these blocks will produce identical switch logic.
{case 1 break}
Code 1
{case 2}
Code 2
{default break}
Code 3

{case 1}
Code 1
{break}
{case 2}
Code 2
{default}
Code 3
{break}


03/08/2008 - Updated to version 2.1
Added {/case} tag, this is identical to {break}.

1 comment:

Jeremy said...

Hi. any chance on seeing this upgraded for Smarty 3.x. Doesn't seem to be working.

thanks!