|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
devnul Smarty Rookie
Joined: 26 Aug 2003 Posts: 32
|
Posted: Mon Oct 06, 2003 9:59 am Post subject: Associative array in {section}{/section}... |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Oct 06, 2003 10:06 am Post subject: |
|
|
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 |
|
devnul Smarty Rookie
Joined: 26 Aug 2003 Posts: 32
|
Posted: Mon Oct 06, 2003 10:21 am Post subject: |
|
|
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 |
|
Dhurgan Smarty n00b
Joined: 09 Oct 2003 Posts: 2 Location: Sundsvall
|
Posted: Thu Oct 09, 2003 12:39 pm Post subject: Are we sure about this? |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Oct 09, 2003 4:21 pm Post subject: |
|
|
use {foreach} for associative arrays. {section} only works if your keys are integers. |
|
Back to top |
|
Dhurgan Smarty n00b
Joined: 09 Oct 2003 Posts: 2 Location: Sundsvall
|
Posted: Fri Oct 10, 2003 7:56 am Post subject: |
|
|
Thanks, I been staring at the section call to long I guess.
/Dhurgan |
|
Back to top |
|
|
|
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
|