|
Posted by "messju mohr" on 12/23/05 12:53
On Fri, Dec 23, 2005 at 11:19:21AM +0200, Kaloyan Tsvetkov wrote:
> I need some help from someone who's got more experience than me with
> Smarty. I've been tasked to create a set of block functions for
> rendering tables. Here's what the syntax look like:
>
> ---proba.html---
>
> {list data=$rows}
>
> {head}
> {column}1{/column}
> {column}2{/column}
> {column}3{/column}
> {/head}
>
> {rows}
> {cell}
> 0. {$SKU} {$data.SKU}
> {/cell}
> {cell} 0. {$brand} {$data.brand} {/cell}
> {cell} 0. {$model} {$data.model} {/cell}
> {/rows}
> {/list}
>
> ---end-of-file---
>
> The $rows data is somethink like that:
>
> ---proba.php---
> .....
>
> $smarty->assign('rows',
> array(
> array(
> SKU => 1,
> brand => 11,
> model => 11,
> ),
>
> array(
> SKU => 2,
> brand => 22,
> model => 22,
> ),
> )
> );
>
> ....
> ---end-of-file---
>
> I need to make the smarty_block_rows() block function loop over the
> data in $rows. In the same time I need to access the elements (SKU,
> brand, model) on each loop. The HTML for the block components are read
> from an external file. If you want to take a look at the code, please
> follow this link: http://kaloyan.info/loop.zip.
you can access the data attribute from the {list} tag within the {row} tags.
you have to search through $smarty->_tag_stack to find the enclosing {list}:
for ($i = count($smarty->_tag_stack) - 1; $i >= 0; $i--) {
$tag =& $smarty->_tag_stack[$i];
if ($tag[0] == 'list') {
$data = $tag[1]['data'];
break;
}
}
/* if $data is set here, then we found an enclosing {list} that has a
data attribute set */
then you have to loop yourself over the $data by setting $repeat at each call to {row}
/* we use the formerly unused $tag[2] to save the current row's count */
if (!isset($tag[2])) {
$tag[2] = 0;
} else {
$tag[2]++;
}
$i = $tag[2]; /* index of the current element */
if (isset($data[$i])) {
/* there are rows left, we assign one
this row will be used in the next iteration of the block function */
$smarty->assign('data', $data[$i]); /* if you want to acces {$data.SKU} */
$smarty->assign($data[$i]); /* if you want to acces {$SKU} */
$repeat = true;
} else {
/* we looped through $data and are done */
$repeat = false;
}
/* always return the block content's output unmodified
it contains the last iteration's output.
*/
return $content;
}
HTH
messju
> Thanks in advance to all which will provide any help, comment or
> advice.
>
> K.
Navigation:
[Reply to this message]
|