View previous topic :: View next topic |
Author |
Message |
TofuWarrior Smarty Rookie
Joined: 05 Jun 2006 Posts: 18
|
Posted: Tue Mar 27, 2007 10:28 am Post subject: referencing associative array using index from section |
|
|
Hi All,
I have an array of objects.
I need to iterate through in reverse.
This doesn't work.
The test to display the index works fine but my referencing of the document doesn't show up anything . I would expect it to print Object.
Code: |
{section name=document loop=$navDocsArray step=-1}
{$smarty.section.document.index}
{$navDocsArray[$smarty.section.document.index]}
{/section}
|
Anyone see how I am being dumb?
Thanks,
TW _________________ We make a living by what we get.
We make a life by what we give. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Mar 27, 2007 5:24 pm Post subject: |
|
|
Your array is 0-based and indexed sequential? And why not just use {$navDocsArray[document]} ? |
|
Back to top |
|
TofuWarrior Smarty Rookie
Joined: 05 Jun 2006 Posts: 18
|
Posted: Wed Mar 28, 2007 9:29 am Post subject: |
|
|
Hi Boots,
Think I tried that and it didn't work either but if that should work then I guess I need to check out what else might be going wrong at least I know what should work.
Thanks,
Paul _________________ We make a living by what we get.
We make a life by what we give. |
|
Back to top |
|
sumer Smarty n00b
Joined: 28 Mar 2007 Posts: 3
|
Posted: Wed Mar 28, 2007 5:29 pm Post subject: |
|
|
deleted
Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 4 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Mar 28, 2007 7:12 pm Post subject: |
|
|
Smarty doesn't do var-vars. Use an array instead.
{$apple[x][i].name}
Or pack your apple vars into an array. Then use dynamic keys:
{assign var=apple value="apple_`$smarty.section.x.iteration`}
{$pack[$apple][i].name}
[also, maybe look up the special $smarty var in the manual and re-read the {section} docs]
And what the hell is wrong with foreach? The who idea of templates is to separate display concerns from other application concerns. As a consequence, you typically simplify the data you send to the template so as to make it easier to use and access. If you take a moment to create some less obtuse arrays to work with, you won't spend so much struggling in your templates or writing ugly template code.
Best. |
|
Back to top |
|
sumer Smarty n00b
Joined: 28 Mar 2007 Posts: 3
|
Posted: Wed Mar 28, 2007 9:42 pm Post subject: |
|
|
deleted
Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 5 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Mar 29, 2007 12:20 am Post subject: |
|
|
I never told you to do it that way. I suggested:
Code: | $apples = array();
...
$apples[$x] = ${"apple_$x"}; // or whatever
$smarty->assign('apples, $apples);
...
{$apples[x][i].info}
|
Like I said: the more you fight against the way Smarty actually does work, the more problems you will have. |
|
Back to top |
|
sumer Smarty n00b
Joined: 28 Mar 2007 Posts: 3
|
Posted: Thu Mar 29, 2007 1:33 pm Post subject: |
|
|
deleted
Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 2 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Mar 29, 2007 6:14 pm Post subject: |
|
|
Who said to assign $apples in a loop? Build your array then assign it. Otherwise use append(). And please stop with the funky formatting. It hurts my eyes and it makes just ignore the message. In fact, I didn't even read past the first 3 lines.
...and please try to avoid things like eval....it seems like all the things you are trying are making your work harder than it needs be. |
|
Back to top |
|
|