Re: [SMARTY] register_function and Objects
| From: | Nick Charsley | Date: | Tue, 27 Aug 2002 15:50:36 +0000 |
| Subject: | Re: [SMARTY] register_function and Objects | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5843@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: <netshade@brainrazor.com>
To: <smarty-general@lists.php.net>
Sent: Tuesday, August 27, 2002 11:44 AM
Subject: [SMARTY] register_function and Objects
> After a little experimenting using the syntax PHP follows in Object method
> attachment/evaluation ( ie. if a function name is required as a string,
you
> may send Array(&$obj,"method_name") instead if you wish to use an object
> method ) I've come to an impass insofar as register object methods as
Smarty
> functions. Is there a solution previously known? A quick hack I can
> perform? Or do I need to fuck around with the source myself? :-)
>
This is how I've done it....
<?php
class clsMatch
{
function Week()
{
....
}
function sm_GamesWon($params)
{
extract($params);
return $this->GamesWon($team);
}
function GamesWon($team)
{
....
}
}
function sm_Match($params)
{
//Called from smarty so
extract($params);
echo $class->$property();
}
function sm_MatchWithParams($params)
{
//Called from smarty so
extract($params);
$property = 'sm_'.$property;
echo $class->$property($params);
}
$smarty->register_function('Match' ,'sm_Match');
$smarty->register_function('MatchParams','sm_MatchWithParams');
?>
I always add 'sm_' to remind me that a function is being called from smarty
and so the parameters will need to be extracted. These are properties as
they actually return something to be output.
Then create an object and assign it to smarty
<?php
$match=new clsMatch();
$smarty->assign('match',$match);
?>
Then in the template ...
<!-- Call Method with no parameters -->
Week {Match class=$match property='Week'}
<!-- Call Method with parameters -->
Games Won {MatchParams class=$match property='GamesWon' team=0}
<!-- I always put the parmeters after the property name -->