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

Accessing {$object->array["variable"]}

 
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
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Wed Jul 02, 2003 6:12 pm    Post subject: Accessing {$object->array["variable"]} Reply with quote

I assigned the object in PHP to this template, but when I try to access an variable in an array in the object I get an error.

Code:

This is the output: {$PageObject->arrContent.content_text}


But I get:
Error message: Smarty: [in pages.tpl line 7]: syntax error: unrecognized tag: $PageObject->arrContent.content_text (Smarty_Compiler.class.php, line 381)
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Jul 02, 2003 6:30 pm    Post subject: Reply with quote

You may have to do that with an intermediate assignment:

{assign var=temp value=$PageObject->arrContent}
{$temp.content_text}

though I suspect that will introduce a performance issue with PHP causing the array to be copied during the assign to the temp var.

Interesting. I'm not going to bother to try, but does this work?

{$PageObject->arrContent['content_text']}
Back to top
View user's profile Send private message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Wed Jul 02, 2003 6:41 pm    Post subject: Reply with quote

boots wrote:
Interesting. I'm not going to bother to try, but does this work?

{$PageObject->arrContent['content_text']}



No, that doesn't work either. I tried several ways to do this but I found none that works.

Your assign code works, but as you said it might introduce some perf. issues because to the array copying.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Jul 03, 2003 8:46 am    Post subject: Reply with quote

use smarty 2.5.0:
{$PageObject->arrContent.content_text} works as expected for arrays.
{$PageObject->arrContent['content_text']} works as expected for section indices.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ze
Smarty Rookie


Joined: 13 Jun 2003
Posts: 5
Location: Rennes, France

PostPosted: Thu Jul 03, 2003 11:51 am    Post subject: Reply with quote

Try to assign the object with assign_by_ref

Code:
$smarty->assign_by_ref('name_of_var',$name_of_php_object);

_________________
--
Yann RICHARD
Back to top
View user's profile Send private message
oschonrock
Smarty Rookie


Joined: 08 Aug 2003
Posts: 13

PostPosted: Fri Aug 08, 2003 8:02 pm    Post subject: Reply with quote

and what about the reverse case? I mean using an object property as an array index.

Code:

    {if $expanded_property_id[$property->id] eq 1}


I can make it work with an intermediate assign like this

Code:

    {assign var='property_id' value=$property->id}
    {if $expanded_property_id[$property_id] eq 1}


but is that necessary? or is there a magic syntax/ backtick trick etc?

Thanks

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


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

PostPosted: Fri Aug 08, 2003 8:14 pm    Post subject: Reply with quote

@oschonrock: unfortunately (to my knowledge) there is no trick to do this without an intermediate assign.

smarty's syntax would need bracketing to control operator-precedence to achieve what you (and i sometimes Smile ) want. and it's a pain in the ass to do sth. like that within a regexp-based parser Sad .
Back to top
View user's profile Send private message Send e-mail Visit poster's website
oschonrock
Smarty Rookie


Joined: 08 Aug 2003
Posts: 13

PostPosted: Fri Aug 08, 2003 9:08 pm    Post subject: Reply with quote

ok, thanks for quick reply, at least we know now.
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