Re: [SMARTY] Creating custom loop block function

From: Date: Fri, 23 Dec 2005 11:37:06 +0000
Subject: Re: [SMARTY] Creating custom loop block function
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-13196@lists.php.net to get a copy of this message
On Fri, Dec 23, 2005 at 01:27:50PM +0200, Kaloyan Tsvetkov wrote: > The problem is that the Smarty variable calls({$SKU}, or {$data.SKU}, or > whatever is the propper Smarty syntax) from {cell}..{/cell} are not working. > These {cell} tags are parsed only one time, and we can not put new values in > them each time we need a new row parsed. These {cell} tags are parsed even > before the first smarty_block_list() call - check the compiled version of > the template for proof. wrong. reread http://smarty.php.net/manual/en/plugins.block.functions.php and maybe reread your compiled template. > How Smarty works (correct me if I am wrong about something) - first it > analyzes the template (smarty syntax), then it creates the compiled version > of the template (php script in the smarty_compiled folder), then this > compiled version is included and run. Running the included compiled version > triggers the block functions that are declared in them. > > When we use {section} or {foreach} (which are core Smarty plugins), Smarty > adds special code in the compiled versions, which handle the access to data > for each iteration loop. That's what we lack when we do our {row} block tag. wrong. see above. > On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote: > > > > 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. > >

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