Hey, I was wondering if it was possible to pass an associative array as a parameter in a custom function. This is my scenario:
嘿,我想知道是否可以将关联数组作为自定义函数中的参数传递。这是我的情景:
In the php file I set the array:
在php文件中我设置了数组:
$dataArr = array('one'=>'1','two'=>'2','three'=>'3');
$tpl->assign('dataArr',$dataArr);
This is my custom function dulled down
这是我的自定义功能变得迟钝
function smarty_function_drawChart($params, &$smarty){
print_r($params);
}
This is my function call in the template
这是我在模板中的函数调用
{drawChart data={$dataArr} title='Title of the Chart'}
The problem I am having is that if you notice where I print_r($params), that shows:
我遇到的问题是,如果你注意到我在哪里print_r($ params),那表明:
Array
(
[data] => Array
[title] => Title of the Chart
)
It seems to be passing the string 'Array' rather than the actual array. I have done debugging right before passing the $dataArr that shows {$dataArr.one} has a value. Once inside my custom function $params['data'].one does not exist.
它似乎传递字符串'Array'而不是实际的数组。在传递显示{$ dataArr.one}的$ dataArr之前,我已经完成了调试。一旦进入我的自定义函数$ params ['data']。一个不存在。
Any ideas on what I am doing wrong?
关于我做错的任何想法?
Thanks
Levi
谢谢列维
1 个解决方案
#1
0
I am still not 100% sure why my code above didn't work. My thought is that the brackets work just as an 'echo' would do in php, which is why the string 'Array' was being passed into the function. I was able to get it to work by simple removing the brackets around the $dataArr variable.
我仍然不能100%确定为什么我上面的代码不起作用。我的想法是括号在php中就像'echo'一样工作,这就是字符串'Array'被传递到函数中的原因。通过简单地删除$ dataArr变量周围的括号,我能够使它工作。
This was my original call:
这是我原来的电话:
{drawChart data={$dataArr} title='Title of the Chart'}
This is my new call that works
这是我的新电话
{drawChart data=$dataArr title='Title of the Chart'}
#1
0
I am still not 100% sure why my code above didn't work. My thought is that the brackets work just as an 'echo' would do in php, which is why the string 'Array' was being passed into the function. I was able to get it to work by simple removing the brackets around the $dataArr variable.
我仍然不能100%确定为什么我上面的代码不起作用。我的想法是括号在php中就像'echo'一样工作,这就是字符串'Array'被传递到函数中的原因。通过简单地删除$ dataArr变量周围的括号,我能够使它工作。
This was my original call:
这是我原来的电话:
{drawChart data={$dataArr} title='Title of the Chart'}
This is my new call that works
这是我的新电话
{drawChart data=$dataArr title='Title of the Chart'}