|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Biggerskimo Smarty n00b
Joined: 23 Jul 2011 Posts: 1
|
Posted: Sat Jul 23, 2011 11:54 pm Post subject: Foreach does not work with instances of IteratorAggregate |
|
|
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 |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Sun Jul 24, 2011 9:27 am Post subject: |
|
|
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 |
|
|
|
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
|