V4n1ll4
V4n1ll4

Reputation: 6099

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

Upvotes: 0

Views: 3065

Answers (2)

V4n1ll4
V4n1ll4

Reputation: 6099

I managed to get it working using smarty assign_by_ref

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

Upvotes: 0

Keith Mifsud
Keith Mifsud

Reputation: 1618

// In smarty you can loop through your array like this:

{foreach from=$results item=result}
{$result->id}
{/foreach}

// You can also use "key=xx" and a "{foreachelse}" before the end of the loop in case there's nothing inside the array.

Upvotes: 2

Related Questions