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

$arr.$foo->bar produces incorrect code

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
whiteatom
Smarty n00b


Joined: 04 Nov 2006
Posts: 2

PostPosted: Sat Nov 04, 2006 7:01 pm    Post subject: $arr.$foo->bar produces incorrect code Reply with quote

Using an associative array, and passing the index value as part of an object, the cache file has an error in it (generates a php "Illegal offest" error).

PHP Code (simplified):
Code:
$smarty->assign('wo_types', array('type1'=>'Type 1', 'type2'=>'Type 1'));
$smarty->assign('wo', $wo_manager->getwobyid($id));

$wo_manager->getwobyid($id) returns an object with a attribute which in this example contains 'type2'.

Template Code:
Code:
{$wo_types.$wo->type}


PHP Produced in Cache:
Code:
echo $this->_tpl_vars['wo_types'][$this->_tpl_vars['wo']]->type


HTML Output:
Code:
Warning: Illegal offset type in


Expected PHP in Cache (I think?):
Code:
echo $this->_tpl_vars['wo_types'][$this->_tpl_vars['wo']->type]


Expected HTML Output:
Code:
Type 2


Thoughts? I use this functionanlity a fair bit becuase I use lookup arrays to store user-friendly text in-place of database codes. It would be great to do it in one step.. so I tried to recreate $wo_types[$wo->type] in smarty and I get this error. This is my current workaround:
Code:
{assign var='type' value=$wo->type}{$wo_types.$type}
Back to top
View user's profile Send private message
Hielke Hoeve
Smarty Elite


Joined: 06 Jan 2006
Posts: 406
Location: Netherlands

PostPosted: Mon Nov 06, 2006 8:25 am    Post subject: Reply with quote

Your work around is the only possible thing I know, unless the early morning is preventing me from thinkig properly Rolling Eyes

whiteatom wrote:
Template Code:
Code:
{$wo_types.$wo->type}


Doesn't this means you want to access the var type in the array called $wo and not the array behind the var $wo? I do k now that there is a limitation in {assign} but not sure due to the too little sleep
_________________
Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net>
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Nov 06, 2006 6:35 pm    Post subject: Reply with quote

That's not a bug. That's a limitation. Smarty has a different precedence than you expected -- but lacks an operator to change precedence (ie: parens) so you are stuck with assigns to get around that. OR perhaps try my lookup modifier (search the forums) which can help get around sticky syntax issues such as these.
Back to top
View user's profile Send private message
whiteatom
Smarty n00b


Joined: 04 Nov 2006
Posts: 2

PostPosted: Tue Nov 07, 2006 2:06 am    Post subject: Reply with quote

Hielke - I'm trying to access the value from the $wo_types array that is keyed by the string contained in the type attribute of the $wo object.. is that clearer???

boots - Ok.. thanks.. an unfortunate limitation as it's one of the few places smarty concepts vary from PHP.

Thanks for the replys.. it looks like I'll continue the way I am going...
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 -> Bugs 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