Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.3.13

Request #3024 [DOC] - note that foreach scopes differently than PHP
Submitted: 2004-12-23 22:29 UTC
From: ryanbell at pobox dot com Assigned:
Status: Verified Package: HTML_Template_Flexy
PHP Version: 4.3.9 OS: FreeBSD 4.10
Roadmaps: (Not assigned)    
Subscription  
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes. If this is not your bug, you can add a comment by following this link. If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
2005-06-27 10:19 UTC
Package:
Bug Type:
Summary:
From: ryanbell at pobox dot com
New email:
PHP Version: Package Version: OS:

 

 [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.

Comments

 [2004-12-30 06:07 UTC] alan_k
what does this refer to? <?php $res = array(array('John','Doe'),array('Jane','Doe')); ?> if you do <?php $t->res = array(array('John','Doe'),array('Jane','Doe')); ?> it should work as expected..
 [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. ^_^