Exponent v2.7.2

Smarty_Compiler_Switch extends Smarty_Internal_CompileBase
in package

Switch statement plugin for smarty.

This smarty plugin provides php switch statement functionality in smarty tags. To install this plugin drop it into your smarty plugins folder. You will also need to manually load the plugin sot hat all the hooks are registered properly. Add the following line after you load smarty and create an instance of it in your source code.


Jeremy Pyne jeremy.pyne@gmail.com

  • Donations: Accepted via PayPal at the above address.

  • Updated: 02/10/2010 - Version 3.2

  • File: smarty/plugins/compiler.switch.php

  • Licence: CC:BY/NC/SA http://creativecommons.org/licenses/by-nc-sa/3.0/

  • Updates Version 2: Changed the break attribute to cause a break to be printed before the next case, instead of before this case. This way makes more sense and simplifies the code. This change in incompatible with code in from version one. This is written to support nested switches and will work as expected. Version 2.1: Added {/case} tag, this is identical to {break}. Version 3: Updated switch statment to support Smarty 3. This update is NOT backwards compatible but the old version is still maintained. Version 3.1: Added a prefilter to re-enable the shorthand {switch $myvar} support. To use the shorthand form you will need to add the following line to your code. $smarty->loadPlugin('smarty_compiler_switch'); Version 3.2: Fixed a bug when chaining multiple {case} statements without a {break}. Version 3.5: Updated to work with Smarty 3.0 release. (Tested and working with 3.0.5, no longer compatible with 3.0rcx releases.) Version 3.6: Updated to work with Smarty 3.1 release. (Tested and working on 3.1.3, No longer compatible with 3.0 releases.)

  • Bugs/Notes:

Sample usage: {foreach item=$debugItem from=$debugData} // Switch on $debugItem.type {switch $debugItem.type} {case 1} {case "invalid_field"} // Case checks for string and numbers. {/case} {case $postError} {case $getError|cat:"_ajax"|lower} // Case checks can also use variables and modifiers. {break} {default} // Default case is supported. {/switch} {/foreach}

Note in the above example that the break statements work exactly as expected. Also the switch and default tags can take the break attribute. If set they will break automatically before the next case is printed.

Both blocks produce the same 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}

Finally, there is an alternate long hand style for the switch statments that you may need to use in some cases.

{switch var=$type} {case value="box" break} {case value="line"} {break} {default} {/switch}


Table of Contents

$optional_attributes  : mixed
$required_attributes  : mixed
$shorttag_order  : mixed
compile()  : string
Start a new switch statement.



public mixed $required_attributes = array('var')



Start a new switch statement.

public compile( $args,  $compiler) : string

A variable must be passed to switch on. Also, the switch can only directly contain {case} and {default} tags.

$args :
$compiler :
Return values

Search results