19

I am working on a website using Smarty. I have searched the web to find out whether or not I can use switch case with Smarty. But i cannot able to find any useful links for this.

Is it possible to use Switch case in Smarty? if so how?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
balanv
  • 10,686
  • 27
  • 91
  • 137

3 Answers3

10

You can find the documentation here: http://www.smarty.net/documentation

No it's not possible (without a plugin). But you can use it in php and assign your results. Or in smarty you can use the if condition instead in a different way.

ggzone
  • 3,661
  • 8
  • 36
  • 59
9

You can also just use a simple if / elsif statement, if you don't like or can install the plugin:

{if $case1_as_condition}
   Case 1
{elseif $case2_as_condition}
   Case 2, etc
{else}
   Default
{/if}

http://www.smarty.net/docsv2/en/language.function.if

Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32
Andreas
  • 1,691
  • 1
  • 15
  • 34
4

Yes with an extra plugin: http://pynej.blogspot.co.uk/2010/02/switch-statment-for-smarty-3.html

{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}
Chris
  • 57
  • 5