RE: [SMARTY] function alternate
| From: | mark priatel | Date: | Thu, 02 May 2002 21:12:22 +0000 |
| Subject: | RE: [SMARTY] function alternate | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-4009@lists.php.net to get a copy of this message | ||
Actually, I wrote a similar plugin a few days ago. {alternate} is
definitely something that should be part of the smarty dist. My version
was similar:
- it accepts values as comma delim list, supporting more than 2 items
- it allows the last value to be returned without advancing to the next
value (useful for when you you have, say, colors alternating every 2
rows)
- it can be reset
I like how yours allows for nesting -- definitely something worth
having.
-mark
function smarty_function_cycle($params, &$smarty)
{
static $values = array();
static $pos;
static $len;
if ( isset($params['values']) ){
$values = split(",",$params['values']);
$pos = 0;
$len = sizeof($values);
return;
}
if ( isset($params['reset']) ){
$pos = 0;
return;
}
if ( isset($params['noadv']) ){
echo $values[$pos];
}else{
$pos++;
if ($pos >= $len) $pos = 0;
echo $values[$pos];
}
}
> -----Original Message-----
> From: Monte Ohrt [mailto:monte@ispi.net]
> Sent: Thursday, May 02, 2002 4:58 PM
> To: Smarty
> Subject: [SMARTY] function alternate
>
> I just created a handy function I'd like your input on, called
alternate.
>
> example:
>
> {section ...}
> <tr>
> <td bgcolor={alternate val1="#eeeeee" val2="#b0b0b0"}>
> .....
> </td>
> </tr>
> {/section}
>
>
> Each time you call alternate, it will alternate printing val1 and
val2.
> In this example, every cell of the table will alternate background
> colors. This is kind of a shortcut for something like this:
>
> {if $smarty.section.foo.index is even}#eeeeee{else}#b0b0b0{/if}
>
> PLUS, if you want to alternate values within one loop of the section
> (like in the above example), you can do so... it is independent of the
> current section loop iteration.
>
> You can optionally supply a "name" attribute in case you have more
than
> one alternate tag running in the template. So you can do this:
>
> {alternate name=foo1 val1="do" val2="re"}
> {alternate name=foo2 val1="mi" val2="fa"}
> {alternate name=foo1 val1="do" val2="re"}
> {alternate name=foo2 val1="mi" val2="fa"}
>
> output would be:
>
> do
> mi
> re
> fa
>
> What I'm wondering, is this syntax good? Are the variable names "val1"
> and "val2" the best choice? Should the alternate function be able to
> accomodate three or more values to "alternate" between?
>
> Monte
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php