accessing associative arrays in smarty templates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

accessing associative arrays in smarty templates

Post by Burrito »

I've found a lot of articles addressing this question, but none with a good concrete answer.

I have an associative array that I'm building from a sql query (yes I'm filling the entire array before sending it to the template, although this shouldn't matter anyway because there's only one row)...and all I want to do is display the value based on the key on the template.

I've tried:

Code: Select all

Project Type: {$folio[projecttype]}
and

Code: Select all

Project Type: {$folio.projecttype}
and not had success with either. I could create a foreach loop and loop over this one row and use the dot syntax, but that seems like waaaay too much overkill to just grab the info out of a single array!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

doh!

typo on my part.

FYI for anyone interested the dot synatx is what you need to use:

ie:

Code: Select all

{$myArr.mykey}
or for multidimensional arrays:

Code: Select all

{$myArr.mySecondArr.mykey}
<!-- etc -->
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea I was thinking... wtf? I'm pretty sure you just use a dot... ?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I use this for loops:

Code: Select all

<?php

$array = array();

for ($i = 0; $i < 5; $i++)
{
    $array[] = array('val' => $i);
}

$smarty->assign('data', $array);

?>

Code: Select all

{section name=i loop=$data}
<p>{$data[i].val}</p>
{/section}
and for future ref:
http://smarty.php.net/crashcourse.php

:)
Post Reply