Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

referencing associative array using index from section

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
TofuWarrior
Smarty Rookie


Joined: 05 Jun 2006
Posts: 18

PostPosted: Tue Mar 27, 2007 10:28 am    Post subject: referencing associative array using index from section Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Mar 27, 2007 5:24 pm    Post subject: Reply with quote

Your array is 0-based and indexed sequential? And why not just use {$navDocsArray[document]} ?
Back to top
View user's profile Send private message
TofuWarrior
Smarty Rookie


Joined: 05 Jun 2006
Posts: 18

PostPosted: Wed Mar 28, 2007 9:29 am    Post subject: Reply with quote

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
View user's profile Send private message
sumer
Smarty n00b


Joined: 28 Mar 2007
Posts: 3

PostPosted: Wed Mar 28, 2007 5:29 pm    Post subject: Reply with quote

deleted

Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 4 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Mar 28, 2007 7:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
sumer
Smarty n00b


Joined: 28 Mar 2007
Posts: 3

PostPosted: Wed Mar 28, 2007 9:42 pm    Post subject: Reply with quote

deleted

Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 5 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Mar 29, 2007 12:20 am    Post subject: Reply with quote

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
View user's profile Send private message
sumer
Smarty n00b


Joined: 28 Mar 2007
Posts: 3

PostPosted: Thu Mar 29, 2007 1:33 pm    Post subject: Reply with quote

deleted

Last edited by sumer on Thu Mar 29, 2007 6:18 pm; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Mar 29, 2007 6:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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