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 does not work with instances of IteratorAggregate

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
Biggerskimo
Smarty n00b


Joined: 23 Jul 2011
Posts: 1

PostPosted: Sat Jul 23, 2011 11:54 pm    Post subject: Foreach does not work with instances of IteratorAggregate Reply with quote

I implemented a class with the IteratorAggregate-Interface:
Code:

<?php
class Foo implements IteratorAggregate
{
   private $bar = array('abc', 'bcd', 'cde');

   public function getIterator()
   {
      return new ArrayIterator($this->bar);
   }
}
$foo = new Foo();
foreach($foo as $value)
{
   echo $value; // prints "abcbcdcde"
}
echo count($foo); // prints "0"!

With Smarty I tried the following:
Code:

{foreach from=$foo item='value'}{$value}{/foreach}

Unfortunately this doesnt print anything. It looks like smarty compiles this to something like
Code:

<?php
if(count($foo) > 0)
{
   foreach($foo as $value)
   {
      echo $value;
   }
}

Since count($foo) is 0 it will not give you the expected result.

Used Version: 3.0.8 with PHP 5.3.5.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sun Jul 24, 2011 9:27 am    Post subject: Reply with quote

Your count($foo) is actually $_smarty_tpl->_count($foo). There you'll find a wrapper to handle all sorts of objects. Just not IteratorAggregate. Smarty 3.1 will also handle IteratorAggregate.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
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 -> Bugs 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