View previous topic :: View next topic |
Author |
Message |
plockaby Guest
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 17, 2003 7:17 pm Post subject: |
|
|
It's a controversial feature 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 |
|
Guest
|
Posted: Thu Apr 17, 2003 7:19 pm Post subject: Re: "switch" template function |
|
|
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
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
|
Posted: Thu Apr 17, 2003 7:20 pm Post subject: |
|
|
I agree |
|
Back to top |
|
plockaby Guest
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 17, 2003 8:35 pm Post subject: Re: "switch" template function |
|
|
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
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Apr 17, 2003 9:03 pm Post subject: Re: "switch" template function |
|
|
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... )
i suggested this, to be able to nest switch statements easily. maybe
it can be made distinct without the name. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 18, 2003 10:17 am Post subject: |
|
|
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.
after investigation i found the problem not in my function, but in Smarty-CVS
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
messju |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 18, 2003 10:32 am Post subject: |
|
|
grmpf. and it emits notices with force_compile=false and debugging=true, but i may leave this to the inclined readers at home. |
|
Back to top |
|
|