|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
pitbull82 Smarty Rookie
Joined: 01 Feb 2006 Posts: 18 Location: Poland
|
Posted: Tue Jul 29, 2014 6:11 am Post subject: Accessing class array property with variables |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 29, 2014 10:24 pm Post subject: |
|
|
Sorry, there is currently no other workaround. |
|
Back to top |
|
AmandaPratt184 Smarty Rookie
Joined: 02 Jan 2015 Posts: 5
|
Posted: Wed Feb 25, 2015 6:00 am Post subject: Any new roll outs? |
|
|
Are there any inputs or new roll outs expected to work around this challenge? |
|
Back to top |
|
|
|
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
|