Page 1 of 1

accessing associative arrays in smarty templates

Posted: Mon Sep 25, 2006 1:16 pm
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!

Posted: Mon Sep 25, 2006 1:30 pm
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 -->

Posted: Mon Sep 25, 2006 1:41 pm
by Luke
yea I was thinking... wtf? I'm pretty sure you just use a dot... ?

Posted: Mon Sep 25, 2006 2:33 pm
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

:)