PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » HTML » HTML_Template_Flexy » Bug #3024

[DOC] - note that foreach scopes differently than PHP

Details

Request #3024[DOC] - note that foreach scopes differently than PHP
Submitted2004-12-23 22:29 UTC
Fromryanbell at pobox dot com
StatusVerified
PackageHTML_Template_Flexy
PHP Version4.3.9
OSFreeBSD 4.10
Roadmaps(Not assigned)

Comments

[2004-12-23 22:29 UTC] ryanbell at pobox dot com

Description:
------------
I am using Flexy v1.1.0. I have a two-dimensional array and would like to do the following (where I actually ran into this was a little different, but it was easier to explain this way ^_-):

<table>
<tr flexy:foreach="res,row">
<td flexy:foreach="row,col">{col}</td>
</tr>
</table>

instead I have to use:

<table>
{foreach:res,row}
<tr>
{foreach:row,col}
<td>{col}</td>
{end:}
</tr>
{end:}
</table>

The more verbose method works, so my immediate problem is solved, but I would like to see the ability to use nested 'flexy:foreach' statements.

Great job with this package, and thanks for all your hard work! ^_^

Reproduce code:
---------------
<?php
$res = array(array('John','Doe'),array('Jane','Doe'));
?>

<table>
<tr flexy:foreach="res,row">
<td flexy:foreach="row,col">{col}</td>
</tr>
</table>

Expected result:
----------------
+------+-----+
| John | Doe |
+------+-----+
| Jane | Doe |
+------+-----+

Actual result:
--------------
[nothing], because nothing gets assigned to 'col'. I imagine this is because $row = $t->res[$i] and $col = $t->row[$i] when using 'flexy:foreach' within the tags, rather than $col = $row[$ii].

Hope that makes sense.

[2004-12-30 17:57 UTC] ryanbell at pobox dot com

Sorry, I meant to write $t->res = ...

Actually, I've taken a fresh look after the holiday break and I think I know what I did wrong. 'flexy:foreach="res,row"' uses the current tag and it's children to establish it's scope. If you try and refer to 'row' outside of that scope, it will be NULL. In my case I was using something similar to the following:

<h3 flexy:foreach="res,row">{row[0]}</h3>
<ul>
<li flexy:foreach="row,col">{col}</li>
</ul>

In which case the use of 'row' in the <li> tag is already out of scope.

If this is the case, I apologize for wasting your time, and instead request an addition to the documentation. After rereading the documentation for this feature I can now see that you do mention 'creates a foreach loop, around the tag and close tag.' I request that you add the following, or similar, to make this description even more clear: 'The scope of any created variables is only within this tag or any children of this tag.'

Also, an example of how NOT to use the variables would make the usage even more clear:

<table>
<tr flexy:foreach="a,k,v">
<td>k is {k}, and v is {v}</td>
</tr>
<tr>
This will not print {k} or {v}, they are out of scope
</tr>
</table>

These are just my humble suggestions. Thank you for your time. ^_^