Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Added a switch command in the core of smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
OrsE
Smarty n00b


Joined: 14 Mar 2005
Posts: 1

PostPosted: Mon Mar 14, 2005 8:13 pm    Post subject: Added a switch command in the core of smarty Reply with quote

I've just coded a switch command like in PHP, it support"default" and "break"
To have this, you must add two fonctions in Smarty_Compiler.class.php:
[php:1:570d47c1b1]<?php
/**
* Compile {switch ...} tag.
*
* @param string $tag_args
* @return string
*/
function _compile_switch_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$arg_list = array();

if (empty($attrs['from'])) {
return $this->_syntax_error("switch: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$from = $attrs['from'];

$output .= "<?php switch($from): ?>";

return $output;
}

/**
* Compile {case ...} tag.
*
* @param string $tag_args
* @return string
*/
function _compile_case_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$arg_list = array();

if (empty($attrs['value'])) {
return $this->_syntax_error("switch: missing 'value' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$from = $attrs['value'];
$output .= "<?php case $from: ?>";

return $output;
}
?>[/php:1:570d47c1b1]

...and add some code in the function _compile_tag on the same file. There is a switch ($tag_command) {, add the code below it:
[php:1:570d47c1b1]<?php
case 'switch':
$this->_push_tag('switch');
return $this->_compile_switch_tag($tag_args);
case '/switch':
list($_open_tag) = end($this->_tag_stack);
if($_open_tag == 'case')
$this->_pop_tag('case');
$this->_pop_tag('switch');
return "<?php endswitch; ?>";
case 'case':
list($_open_tag) = end($this->_tag_stack);
if($_open_tag != 'case' && $_open_tag != 'switch')
$this->_syntax_error('unexpected {case}, missing previous case or switch', E_USER_ERROR, __FILE__, __LINE__);
if ($_open_tag != 'case')
$this->_push_tag('case');
return $this->_compile_case_tag($tag_args);
case '/case':
list($_open_tag) = end($this->_tag_stack);
if($_open_tag != 'case')
$this->_syntax_error('unexpected {/case}, missing previous case', E_USER_ERROR, __FILE__, __LINE__);
$this->_pop_tag('case');
return "<? break; ?>";
case 'default':
list($_open_tag) = end($this->_tag_stack);
if($_open_tag == 'case')
$this->_pop_tag('case');
return "<? default: ?>";

?>[/php:1:570d47c1b1]

Now you can use switch, case, break and default:
[php:1:570d47c1b1]<?php
$value = 2;
$string= "chaine";
$smarty->assign("val",$value);
$smarty->assign("string",$string);
?>[/php:1:570d47c1b1]

in the template:
code 1:
Code:

{switch from=$val}
{case value=1}
   val=1;<br>
{case value=2}
   val=2;<br>
{case value=3}
   val=3;<br>
{case value=4}
   val=4;<br>
{default}
   default value!!<br>
{/switch}

code 3:
Code:

{switch from=$val}
{case value=1}
      val=1;<br>
      {/case}
   {case value=2}
      val=2;<br>
      {/case}
   {case value=3}
      val=3;<br>
      {/case}
   {case value=4}
      val=4;<br>
      {/case}
   {default}
      default value!!
{/switch}

code 3:
Code:

{switch from=$string}
{case value="chienne"}
string={$string}<br>
{/case}
{case value="chaine"}
string={$string}<br>
{/case}
{case value="chene"}
string={$string}<br>
{/case}
{/switch}


The first code will output:
val=2;
val=3;
val=4;
default value!!

The second:
val=2;

And the third:
string=chaine

{switch from=$val} <-- from is the input variable
{case value=1} <-- value is the value to compare with
{/case} <-- is't the break command, if ommited, the previous case will continue
{default} <-- it's the default part of the witch
{/switch} <-- the end of the switch

If you have some question or idea.. i'am here Wink
If you want to put this code in the next release of Smarty, i'am totally agree Very Happy
bye
_________________
There are 10 kind of people, witch that know binary, and others...
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP