Re: [SMARTY] function: assign_array

From: Date: Fri, 03 May 2002 14:42:35 +0000
Subject: Re: [SMARTY] function: assign_array
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4031@lists.php.net to get a copy of this message
Ok, cycle has been updated to accomodate this. If you leave out the "values" parameter, it will assume the last one used for the current id. So {cycle values="1,2,3"} {cycle} {cycle} {cycle} will output 1 2 3 1 If you _change_ the values for a particular id, the index will be reset. Monte Jason Sweat wrote:
With regards to the cycle function, I think most of the time it would be used in a section to alternate row colors like: {section name=r loop=$rows} <tr bgcolor="{cycle var="row_color" values="white,silver"}"><td>{$row.val}</td></tr> {/section} So each iteration through the section it is always passing the values, and alternative syntax that might be nice to have would be the ability to call after the cycle has been initalized with just the var name. This might be used for column based color changes: {section name=r loop=$rows} <tr > <td bgcolor="{cycle var="col_color" values="white,silver,blue"}">{$row.val1}</td> <td bgcolor="{cycle var="col_color"}">{$row.val2}</td> <td bgcolor="{cycle var="col_color"}">{$row.val3}</td> <td bgcolor="{cycle var="col_color"}">{$row.val4}</td> <td bgcolor="{cycle var="col_color"}">{$row.val5}</td> <td bgcolor="{cycle var="col_color"}">{$row.val6}</td> </tr> {/section} Jason
\Below is the function to assign an array within
a template. Just in case anyone besides me needs it. :-) other topic: I think I found a little bug at function.cycle.php (line 47). I guess the triggered error must be "cycle: missing 'values' parameter" (cycle instead of assign). <?php /* * Smarty plugin * -------------------------------------------------------------
 * Type:     function
 * Name:     assign_array
* Version: 1.0 * Author: Jens Lehmann <jenslehmann@goldmail.de> * Credits: Monte Ohrt <monte@ispi.net> * Purpose: assign an array to a template variable
 * Input:    var       =  name of the array
 *           values    =  list of values (seperated by
delimiter)
 *           delimiter =  value delimiter, default is
"," * * Examples: {assign_array var="foo" values="bar1,bar2"}
 *           {assign_array var="foo"
values="bar1;bar2;bar3" delimiter=";"} * ------------------------------------------------------------- */ function smarty_function_assign_array($params, &$smarty) {
    extract($params);
if(empty($delimiter)) {
    $delimiter = ',';
}
    if (empty($var)) {
        $smarty->trigger_error("assign_array: missing
'var' parameter");
        return;
    }
    if (!in_array('values', array_keys($params))) {
        $smarty->trigger_error("assign_array: missing
'values' parameter");
        return;
    }
    $smarty->assign($var, explode($delimiter,$values)
); } /* vim: set expandtab: */ ?> __________________________________________________ Do You Yahoo!? Yahoo! Health - your guide to health and wellness http://health.yahoo.com
-- Monte Ohrt <monte@ispi.net> Director of Technology, ispi Inc.

« previous php.smarty.general (#4031) next »