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 class array property with variables

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
pitbull82
Smarty Rookie


Joined: 01 Feb 2006
Posts: 18
Location: Poland

PostPosted: Tue Jul 29, 2014 6:11 am    Post subject: Accessing class array property with variables Reply with quote

I'm using 3.1.19 version.

In PHP I have:

Code:

class A {
    public $data = ['key1' => 'val1', 'key2' => 'key2'];
}


$smarty->assign('var','data');

$smarty->assign('a',new A);


In Smarty template I have:

Code:
{$a->data|@var_dump}
<br />

{$a->data['key1']}
<br />

{$a->$var|@var_dump}
{$a->$var['key1']}


Output for this is:


Quote:
array(2) { ["key1"]=> string(4) "val1" ["key2"]=> string(4) "key2" }
val1
array(2) { ["key1"]=> string(4) "val1" ["key2"]=> string(4) "key2" }
Warning: Illegal string offset 'key1' in ...smarty\sysplugins\smarty_internal_templatebase.php(171) : eval()'d code on line 37

Notice: Undefined property: A::$d in ...smarty\sysplugins\smarty_internal_templatebase.php(171) : eval()'d code on line 37


It seems that when property name is set as variable it's not possible to access it as array even if using var_ump shows us the array.


It seems that in that case Smarty tries first to evaluate $var['key1'] and then $a-> result of this evaluation. However I don't know any way to change this order using for example ( - {($a->$var)['key1']} - won't work.


Is there any workaround to do it in Smarty (without modifiers/extra variables) ?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jul 29, 2014 10:24 pm    Post subject: Reply with quote

Sorry, there is currently no other workaround.
Back to top
View user's profile Send private message
AmandaPratt184
Smarty Rookie


Joined: 02 Jan 2015
Posts: 5

PostPosted: Wed Feb 25, 2015 6:00 am    Post subject: Any new roll outs? Reply with quote

Are there any inputs or new roll outs expected to work around this challenge?
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 -> Smarty 3 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