Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

foreach support for iterators

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
mjlivelyjr
Smarty n00b


Joined: 08 Sep 2004
Posts: 1

PostPosted: Wed Sep 08, 2004 3:22 pm    Post subject: foreach support for iterators Reply with quote

Are there any plans in the future to add support for php5 iterators to the foreach function. The current implementation does not work at will with the new iterators. Below is the begining of the current foreach block as it is translated (note I am only using the required attributes from and item.)

Code:

<?php if (count($_from = (array)$this->_tpl_vars['categoryTree'])):
    foreach ($_from as $this->_tpl_vars['parentCat']): ?>


The problem stems from casting the array (or in the case of iterators, the object) as an array. Removing that casting makes it work, but even then it isn't working as documented because the count() function that wraps the whole type-cast would always return 1 if it is passed an object so the {foreachelse} functionality breaks. Therein lies the problem with the PHP iterator. PHP doesn't require you to implement a method that provides the number of items that can be iterated over. Which leaves you with either requiring an additional method such as ->count or creating an entirely new core function/block/whatever to handle the PHP5 Iterator.

If you were to go with the ->count route the following code output seems as though it could work.

Code:

<?php if ((is_object($this->_tpl_vars['categoryTree']) && is_a($this->_tpl_vars['categoryTree'], 'Iterator') && $this->_tpl_vars['categoryTree']->count() && $from = $this->_tpl_vars['categoryTree']) || count($_from = (array)$this->_tpl_vars['categoryTree'])):
    foreach ($_from as $this->_tpl_vars['parentCat']): ?>


Now I realize that's not very elegant and there may even be syntax errors *gasp* but I am in a hurry and am trying to convey an idea.....not an actual solution. One thing above that would need to change eventually to maintain forward compatibility is getting rid of the is_a() call and replacing it with the instanceof operator. In any case I think a solution to this problem would make an excellent addition to the core package. After I beat a few deadlines I would be more than willing to contribute a few 'complete' ideas for solutions.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 08, 2004 5:20 pm    Post subject: Reply with quote

Well, It seems that several attempts have been made to solve this. This has been discussed (I believe probably for inclusion during the 2.7.x branch) but no final solution was found, AFAIK. There are problems, as you point out, especially in the way Smarty defines the foreach loop compared to what PHP provides. It is unfortunate that both is_a and instanceof are not BC to PHP 4.06 as there is a long standing strategy to not raise the PHP requirements for Smarty, at least in its current form and for the 2.x branches.

I think some more ideas that considered the BC requirements as well as the FC requirements are in order Smile

Cheers!
Back to top
View user's profile Send private message
Elisios
Smarty n00b


Joined: 12 Dec 2004
Posts: 1

PostPosted: Sun Dec 12, 2004 11:13 am    Post subject: Iteration Reply with quote

Hi,

I'm creating a small Database Access Object library and I was wondering how to integrate query result display into Smarty. I don't feel happy to gather all rows into a big array in memory and then passing it to the template, so I was looking to an iteration solution, where row are pulled one by one by the template. (Btw, I'm using PHP 4.2.x).

After reading Smarty documentation in regard to this problem, I see theses solutions :

(a) request this feature : support for something like SmartyIterator interface (which could herit from Iterator in PHP5) into foreach/section loops,

(b) request this feature : a register_loop() similar to register_block() to allow easy creating of custom loop statements :
- register_loop("XXX", ...) could add the following tags : "{XXX name="YYY"}", "{XXXElse}" and "{/XXX}" and adds a smarty special array : {$smarty.loop.XXX.YYY} (or maybe {$smarty.XXX.YYY} to be consistant with foreach and section)

(c) implements (b) as a plugin myself, except that...
- if I use register_block, I don't see how to implement the {XXXelse} tag,
- if I use register_compiler_function, I don't see how to implement the closing tag,
- I don't know how to alter the $smarty array (btw, I can use a var assignment to workaround this problem).

As I see, (a) doesn't fullfil BC requirement policy.

Any opinion about (b) or help about (c) welcome...

Best regards,
_________________
Elisios
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP