Search code examples
phpobjectsmarty

How to loop through an object in smarty


I have a script which is passing an object array to smarty like so:

$smarty->assign('results', $user->results());

The array looks like this:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [agency_name] => Agency #1
        [created_date] => 2015-03-25 20:23:44
    )

[1] => stdClass Object
    (
        [id] => 2
        [agency_name] => gggg
        [created_date] => 2015-03-25 21:26:06
    )

)

How can I loop through this object array using smarty? I have tried something like this, with no luck..

{section name="i" loop=$result}
{$result[i]->id}
{/section}

Any ideas?

Thanks


Solution

  • I managed to get it working using smarty assign_by_ref

    $smarty->assign_by_ref('object', $object)