|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I need some 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. Thanks in advance to all which will provide any , comment or advice. K. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Fri, Dec 23, 2005 at 11:19:21AM +0200, Kaloyan Tsvetkov wrote:
> I need some 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 , comment or > advice. > > K. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. 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.. 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 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 , comment or > > advice. > > > > K. > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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/plug....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 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 , comment or > > > advice. > > > > > > K. > > |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Thank you, I will do that. Are you saying that I have to modify the &$repeat
parameter of the block function to make it loop? What is the correct syntax for addressign smarty vars inside the {cell}...{/cell} block ? Here's my compiled template. Here's how I got the idea for reseting &$repeat to true to keep the while() loop going. -------- <?php /* Smarty version 2.6.10, created on 2005-12-23 12:04:10 compiled from proba.html */ ?> <?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php'); smarty_core_load_plugins(array('plugins' => array(array('block', 'list', ' proba.html', 8, false),array('block', 'head', 'proba.html', 10, false),array('block', 'column', 'proba.html', 11, false),array('block', 'rows', 'proba.html', 16, false),array('block', 'cell', 'proba.html', 17, false),)), $this); ?> <html> <head> <title>List Table Proba</title> </head> <body> <?php $this->_tag_stack[] = array('list', array('data' => $this->_tpl_vars['rows'])); smarty_block_list($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> <?php $this->_tag_stack[] = array('head', array()); smarty_block_head($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> <?php $this->_tag_stack[] = array('column', array()); smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?>1<?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $this->_tag_stack[] = array('column', array()); smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?>2<?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $this->_tag_stack[] = array('column', array()); smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?>3<?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_column($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_head($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $this->_tag_stack[] = array('rows', array()); smarty_block_rows($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> <?php $this->_tag_stack[] = array('cell', array()); smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> 0. <?php echo $this->_tpl_vars['SKU']; ?> <?php echo $this->_tpl_vars['data']['SKU']; ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $this->_tag_stack[] = array('cell', array()); smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> 0. <?php echo $this->_tpl_vars['brand']; ?> <?php echo $this->_tpl_vars['data']['brand']; ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $this->_tag_stack[] = array('cell', array()); smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], null, $this, $_block_repeat=true);while ($_block_repeat) { ob_start(); ?> <?php echo ' 0. {$row_each.brand} '; ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_cell($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_rows($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> <?php $_block_content = ob_get_contents(); ob_end_clean(); echo smarty_block_list($this->_tag_stack[count($this->_tag_stack)-1][1], $_block_content, $this, $_block_repeat=false); } array_pop($this->_tag_stack); ?> </body> </html> -------- On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote: > > 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/plug....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. > > > |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote:
> > > > wrong. reread http://smarty.php.net/manual/en/plug....functions.php > and maybe reread your compiled template. > > > OK, the Smarty docs state that: The value of *$content* variable depends on whether your function is called for the opening or closing tag. In case of the opening tag, it will be null, and in case of the closing tag it will be the contents of the template block. Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source. http://smarty.php.net/manual/en/plug....functions.php So in order to make the {cell} calls work, I have to populate them in advance by the {row}tag, right ? And this has to be done by the opening {row} tag, right ? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Fri, Dec 23, 2005 at 01:59:05PM +0200, Kaloyan Tsvetkov wrote:
> On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote: > > > > > > > > wrong. reread http://smarty.php.net/manual/en/plug....functions.php > > and maybe reread your compiled template. > > > > > > > OK, the Smarty docs state that: > > The value of *$content* variable depends on whether your function is called > for the opening or closing tag. In case of the opening tag, it will be null, > and in case of the closing tag it will be the contents of the template > block. Note that the template block will have already been processed by > Smarty, so all you will receive is the template output, not the template > source. > > http://smarty.php.net/manual/en/plug....functions.php > > So in order to make the {cell} calls work, I have to populate them in > advance by the {row}tag, right ? And this has to be done by the opening > {row} tag, right ? right. that's what I did in my example. or it could be done by the *opening* {cell} tag. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
I did that. But then all my rows are populated with the values of the last
row. For example, the calls from the compiled version to <?php echo $this->_tpl_vars['SKU']; ?> always return the {SKU} from the last row. It seems to me all the loop iterations are done before accessing the first {cell} block. It seems that I am doing something wrong, but I can not figure it out. On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote: > > On Fri, Dec 23, 2005 at 01:59:05PM +0200, Kaloyan Tsvetkov wrote: > > On 12/23/05, messju mohr <messju@lammfellpuschen.de> wrote: > > > > > > > > > > > > wrong. reread > http://smarty.php.net/manual/en/plug....functions.php > > > and maybe reread your compiled template. > > > > > > > > > > > OK, the Smarty docs state that: > > > > The value of *$content* variable depends on whether your function is > called > > for the opening or closing tag. In case of the opening tag, it will be > null, > > and in case of the closing tag it will be the contents of the template > > block. Note that the template block will have already been processed by > > Smarty, so all you will receive is the template output, not the template > > source. > > > > http://smarty.php.net/manual/en/plug....functions.php > > > > So in order to make the {cell} calls work, I have to populate them in > > advance by the {row}tag, right ? And this has to be done by the opening > > {row} tag, right ? > > right. that's what I did in my example. or it could be done by the > *opening* {cell} tag. > |
|
![]() |
| Outils de la discussion | |
|
|