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

Unable to do $obj->arr[sec_name].var?

 
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
chaosone
Smarty n00b


Joined: 24 Jul 2003
Posts: 2
Location: Kent, England

PostPosted: Thu Jul 24, 2003 9:21 pm    Post subject: Unable to do $obj->arr[sec_name].var? Reply with quote

Hi!

I've been using Smarty for almost a year now, and am really liking it. I have only really got on to doing some serious work with it in the past two months, and have come across something that I haven't been able to (satisfactorily) solve reading FAQs, Wiki, searching the forum and such.

I am using Smarty 2.5.0 by the way.

I have started using objects to hold a lot of page information, one piece of which is contained within an array. As an example (from my project) I have a $layout object with a $scripts array in it, with each element of that array holding an array with associative values such as "rel"=>"stylesheet" "href"=>"mystyle.css" etc. What I would like to do is quickly access this array from the Smarty template, after assigning the object to the template object.

Code:

      {section name=count_s loop=$layout->scripts}
      <script rel="{$layout->scripts[count_s].rel}" href="{$layout->scripts[count_s].href}" type="{$layout->scripts[count_s].type}"></script>
      {/section}


The code appears to loop the correct number of times, but all I get between the quotes on the output is emptyness (nothing). So it counts the $layout->scripts array fine, I just can't seem to use it within the section (or even outside of one).

The above syntax looks logical (to me!) I just can't understand if I'm missing something as to why it doesn't work, or if there is a reason this syntax isn't supported (or might it be in the future?) I presume I could do things like assign the $object->array bit to an intermediate template variable, but would very much like to avoid this if possible.

Cheers!
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Jul 24, 2003 9:46 pm    Post subject: Reply with quote

it looks logical to me too and the php-code produced by {$layout->scripts[count_s].rel is also correct. is your script array indexed by 0..n-1 as it has to when used with {section} ?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


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

PostPosted: Thu Jul 24, 2003 9:48 pm    Post subject: Reply with quote

Hi chasone.

AFAIK, that syntax is not supported, at least in 2.5.0 (at least it never works for me). Looking at your code I can suggest two somewhat simple workarounds:

1) Use an intermediate variable. This is a bit messy, though.
Code:
{section name=count_s loop=$layout->scripts}
    {assign var=script  value=$layout->scripts[count_s]}
    <script rel="{$script.rel}" href="{$script.href}" type="{$script.type}"></script>
{/section}


2) Use foreach instead of section. You *may* have to use an intermediate here as well, to represent the from var. I won't bother with that detail as it is more than likely that you won't have to deal with it:
Code:
{foreach item=script from=$layout->scripts}
      <script rel="{$script.rel}" href="{$script.href}" type="{$script.type}"></script>
{/foreach}


As an added benefit, foreach loops tend to be easier to read (brain parse) than sections. I prefer the 2nd option for myself.

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


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

PostPosted: Thu Jul 24, 2003 9:52 pm    Post subject: Reply with quote

'nah boots, the syntax is correct and supported. but i agree with you that foreach simplifies things sometimes really and this case is a good example that it does Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


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

PostPosted: Thu Jul 24, 2003 9:56 pm    Post subject: Reply with quote

My bad. Like I said, it never worked for me, though I never tried too hard since I typically try to structure for foreach anyhow. As I typically use associative arrays, that could have been my problem with the syntax.

Never-the-less, I was unaware that that syntax was available in 2.5.0, so thanks for that. I still suggest the foreach route as it makes for lighter code Smile
Back to top
View user's profile Send private message
chaosone
Smarty n00b


Joined: 24 Jul 2003
Posts: 2
Location: Kent, England

PostPosted: Thu Jul 24, 2003 10:10 pm    Post subject: Reply with quote

Thanks for the speedy response guys, I feel a bit stupid now though because after five hours (at least) trying to find out why Smarty didn't support this syntax and then posting here for some help, I realise it's actually my PHP coding that was buggered up Embarassed

As messju pointed out, the array has to be index by 0...n-1, but because of the way I was adding items to the array for my $layout object, this wasn't happening! Apologies for being a bit brain dead looking at my code, at least it has taught boots a new syntax Smile
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