Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
get array item from custom function

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
matt86
Smarty n00b


Joined: 04 Dec 2012
Posts: 2

PostPosted: Tue Dec 04, 2012 9:23 am    Post subject: get array item from custom function Reply with quote

Hello,
I've a problem with register_function.

I created a function in this way:
Code:

function hello($params)
{
   $a = $params['a'];
   $b = $params['b'];
   return array('A' => $a, 'B' => $b);
}


so, I registered the function in this way:

Code:

$Template->register_function('hello', 'hello');


then in my template I wrote:

Code:

{$foo = {hello a=60 b=30}}
<b>{$foo.B}</b>


the problem is that the template print always "Array" and not the value "30" (in this case)

where I wrong?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Dec 04, 2012 10:33 pm    Post subject: Reply with quote

Note that function plugins are expected to return printable values.

For other cases the standart procedure is that the plugin accepts an assign parameter with the variable which should recieve the result.

Code:
function hello($params, $smarty)
{
   $a = $params['a'];
   $b = $params['b'];
   $smarty->assign( $params['assign'], array('A' => $a, 'B' => $b));
}


Code:
{hello a = 10, b =30, assign = 'foo'}
{$foo.B}
Back to top
View user's profile Send private message
matt86
Smarty n00b


Joined: 04 Dec 2012
Posts: 2

PostPosted: Wed Dec 05, 2012 6:31 am    Post subject: Reply with quote

Great!
now it work!

thanks

U.Tews wrote:
Note that function plugins are expected to return printable values.

For other cases the standart procedure is that the plugin accepts an assign parameter with the variable which should recieve the result.

Code:
function hello($params, $smarty)
{
   $a = $params['a'];
   $b = $params['b'];
   $smarty->assign( $params['assign'], array('A' => $a, 'B' => $b));
}


Code:
{hello a = 10, b =30, assign = 'foo'}
{$foo.B}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development 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