Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Associative array in {section}{/section}...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
devnul
Smarty Rookie


Joined: 26 Aug 2003
Posts: 32

PostPosted: Mon Oct 06, 2003 9:59 am    Post subject: Associative array in {section}{/section}... Reply with quote

Hello,

I am having a bear of a time getting something to work... I've reduced it down to a simple "test".

In test.php:
Code:

  $smarty->assign('cust_options', array(
                        1001 => 'Joe Schmoe',
                        1002 => 'Jack Smith',
                        1003 => 'Jane Johnson',
                        1004 => 'Charlie Brown'));
  $smarty->display('tests/index.tpl');


In tests/index.tpl:

Code:

{section name=customer loop=$cust_options}
HRRM{$cust_options[customer]}<br>
{/section}

I end up with 4 blank lines with "HRRM" on them.

What am I doing wrong here?...

I note that if I change test.php to:

Code:

  $smarty->assign('cust_options', array(
                        0 => 'Joe Schmoe',
                        1 => 'Jack Smith',
                        2 => 'Jane Johnson',
                        3 => 'Charlie Brown'));


I get the output I want (except of course now all my keys are incorrect)...

Any ideas?..

- Greg
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Oct 06, 2003 10:06 am    Post subject: Reply with quote

use start=1001 as a parameter to section. Or make the array associative by using strings for the key names ('1001', '1002'...) and process with a foreach instead of section.
Back to top
View user's profile Send private message
devnul
Smarty Rookie


Joined: 26 Aug 2003
Posts: 32

PostPosted: Mon Oct 06, 2003 10:21 am    Post subject: Reply with quote

Hrrm.... I guess I can't use {section}'s because my id's are all over the map [4,7,12,21,28], for example....

Maybe this should be clarified in the documentation for {section}{/section}?

I have went back and re-read it over-and-over again and even the example which is shown seems to hint very heavily that associative arrays work in {section}'s....

In any event, thanks for your quick response!

- Greg
Back to top
View user's profile Send private message Visit poster's website
Dhurgan
Smarty n00b


Joined: 09 Oct 2003
Posts: 2
Location: Sundsvall

PostPosted: Thu Oct 09, 2003 12:39 pm    Post subject: Are we sure about this? Reply with quote

Hi,

I'm quite new to smarty and are trying to get things to work too, but I also found this matter lacking quite a bit. I have an assosiative array that smarty has no problems displaying in its debug function but that I cant get to work with f.ex. section.

in the php assign
Code:
$ax["BLAH"]="blah blah bla";
$ax["DUH"]="Duh! a new dork";
$ax["KK"]="Okelidokeli";
$smarty->assign(axarr,$ax);

and in the template...
Code:
{section name=i loop=$axarr}
<br>{$axarr[i].key}:{$axarr[i].value}
{/section}

I would have expected this to work since thats how php stores it, but it doesnt.
However, if I do a call to debug a line below the section, it pops a debug window on me showing that it does understand how to display the array quite well, although I cant find any way to do it in smarty myself, I can code around this "limitation" but I shouldnt need to, should I?

Anyone knows how to get at the indexes of the assosiative array to loop them?

/Dhurgan
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Oct 09, 2003 4:21 pm    Post subject: Reply with quote

use {foreach} for associative arrays. {section} only works if your keys are integers.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dhurgan
Smarty n00b


Joined: 09 Oct 2003
Posts: 2
Location: Sundsvall

PostPosted: Fri Oct 10, 2003 7:56 am    Post subject: Reply with quote

Embarassed Thanks, I been staring at the section call to long I guess.

/Dhurgan
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP