Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Accessing class array property with variables

 
Post new topic   Reply to topic    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: 4672
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
Display posts from previous:   
Post new topic   Reply to topic    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