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

"switch" template function

 
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
plockaby
Guest





PostPosted: Thu Apr 17, 2003 6:46 pm    Post subject: "switch" template function Reply with quote

I requested this on the DEV mailing list and heard no reply. So I'll try it again here. I want a function similar to php's switch to be available to template designers. It would remove the necessity for tons of "if-then-else" statements and make templates look a lot cleaner. Anyone else think that this is a good idea?

Last edited by plockaby on Wed May 05, 2010 10:49 pm; edited 1 time in total
Back to top
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 17, 2003 7:17 pm    Post subject: Reply with quote

It's a controversial feature Smile I can see where this can be handy, but it also promotes more complex logic in the the templates. Typically you can get around the lists of IF statements. For instance, if you are determining which template to display depending on an assigned variable, that could be done an easier way:

Code:

{if $page eq "front"}
   {include file="front.tpl"}
{elseif $page eq "sports"}
   {include file="sports.tpl"}
...
{/if}


could be written as:

Code:

{include file=$page_tpl}


Where page_tpl is the name of the template to display.
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Thu Apr 17, 2003 7:19 pm    Post subject: Re: "switch" template function Reply with quote

plockaby wrote:
I requested this on the DEV mailing list and heard no reply. So I'll try it again here. I want a function similar to php's switch to be available to template designers. It would remove the necessity for tons of "if-then-else" statements and make templates look a lot cleaner. Anyone else think that this is a good idea?


i don't think that it would look that better than a bunch of elseifs.
but: do you mean sth. like:

{switch name=foo value=$var}
...
{case name=foo value="c1"}
...
{case name=foo value="c2"}
...
{case name=foo} {* default *}
...
{/switch}

??

i think i can hack this together with 3 compiler-functions ("switch" "case" and "/switch") without the need for touching smartie's core.

just give me a few of these 25hrs-days Smile

honestly: if more people feel the need of it, i'll do it as set of functions, but it will stay smarty-contrib.
i think {if}-{elseif}--{else} is enough for smarty-core.

greetings
messju
Back to top
Tom Sommer
Administrator


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Thu Apr 17, 2003 7:20 pm    Post subject: Reply with quote

I agree
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
plockaby
Guest





PostPosted: Thu Apr 17, 2003 7:47 pm    Post subject: Reply with quote

Nuts. I'll just have to put together something on my own.

Last edited by plockaby on Wed May 05, 2010 10:49 pm; edited 1 time in total
Back to top
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Apr 17, 2003 8:35 pm    Post subject: Re: "switch" template function Reply with quote

Anonymous wrote:

i don't think that it would look that better than a bunch of elseifs.
but: do you mean sth. like:

{switch name=foo value=$var}
...
{case name=foo value="c1"}
...
{case name=foo value="c2"}
...
{case name=foo} {* default *}
...
{/switch}

??

i think i can hack this together with 3 compiler-functions ("switch" "case" and "/switch") without the need for touching smartie's core.

just give me a few of these 25hrs-days Smile

honestly: if more people feel the need of it, i'll do it as set of functions, but it will stay smarty-contrib.
i think {if}-{elseif}--{else} is enough for smarty-core.

greetings
messju


I agree with messju, I don't see the need for this in the core. If you are going to create a set of customs, though, I was wondering what the name attrib was for. I think the main purpose was to make this clean. If nothing else, it should hide the underlying language details, yes? Hows about:

{menu choice=$item}
{is 1}
{* I think this syntax would have to be a compiler function}

{is > 1}

{is any}
{* default *}
{/menu}


I just don't know if its really worth it, though.

xo boots
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Apr 17, 2003 9:03 pm    Post subject: Re: "switch" template function Reply with quote

boots wrote:
I agree with messju, I don't see the need for this in the core. If you are going to create a set of customs, though, I was wondering what the name attrib was for. I think the main purpose was to make this clean. If nothing else, it should hide the underlying language details, yes? Hows about:

{menu choice=$item}
{is 1}
{* I think this syntax would have to be a compiler function}

{is > 1}

{is any}
{* default *}
{/menu}


I just don't know if its really worth it, though.

xo boots



( i managed to log in now, i am soo good... Smile )

i suggested this, to be able to nest switch statements easily. maybe
it can be made distinct without the name.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 18, 2003 10:17 am    Post subject: Reply with quote

plockaby wrote:
Nuts. I'll just have to put together something on my own.


i tried too. i didn't even need it, i just wanted to do some dirty hacking to relax a bit, but i soon started pulling my hair out. Sad
after investigation i found the problem not in my function, but in Smarty-CVS Sad
anyway this one should work with 2.5.0 and with latest CVS now:

Code:

<?php

$this->register_compiler_function('/select', 'smarty_compiler_select_close');
$this->select_stack = array();
$this->select_top = -1;

function smarty_compiler_select($tag_args, &$this) {
    /* handle {select ...} */

    $_attrs = $this->_parse_attrs($tag_args);
    $_ret = '';

    if (isset($_attrs['var'])) {
        /* handle new switch */
        $this->select_stack[++$this->select_top] = array('open'=>false, 'var'=>$_attrs['var']);
        $_ret .= "\$this->select_stack[++\$this->select_top] = array('matched'=>false);";

    } elseif (isset($_attrs['value'])) {
        /* handle new case */
        $_data =& $this->select_stack[$this->select_top];
        if ($_data['open']) $_ret .= " }\n";
        $_ret .= "if (".$_data['var']." == ".$_attrs['value'].") { ";
        $_ret .= "\$this->select_stack[\$this->select_top]['matched']=true;";
        $_data['open'] = true;

    } else {
        /* handle default */
        $_data =& $this->select_stack[$this->select_top];
        if ($_data['open']) $_ret .= " }\n";
        $_ret .= "if (!\$this->select_stack[\$this->select_top]['matched']) { ";
        $_data['open'] = true;
    }

    return $_ret; 
}

   
function smarty_compiler_select_close($tag_args, &$this) {
    /* handle {/select} */

    $_ret = '';
    $_data =& $this->select_stack[$this->select_top--];
    if ($_data['open']) $_ret .= " } ";
    $_ret .= "\$this->select_top--; ";
    return $_ret;

}

?>



usage:

Code:

{select var=$foo}

{select value=1}
one

{select value=2}
two

{select}
i don't know

{/select}



this one does some nasty tricks and makes a few assumptions about how smarty
handles compiler-functions *today*. so maybe it's nothing for before lunch and nothing
to build your business upon.

anyway
have fun Smile

messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 18, 2003 10:32 am    Post subject: Reply with quote

grmpf. and it emits notices with force_compile=false and debugging=true, but i may leave this to the inclined readers at home. Smile
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