Hey, Looking for some clarification on how plugins work.
From what I can see you have the Class Name, a constructor and methods.
eg:
class User { //Start Class
var $name="";
var $email = "";
function User(){ //constructor
//Do some stuff, maybe access $SESS to get currently logged in user or query a database.
//For the mean time we will just hard code name and email
$this->name = "John";
$this->email = "[email protected]";
}
function getName(){ //Method
return $this->name;
}
function getEmail(){ //Method
return $this->email;
}
} //End Class
Now, I want to access this in my template.
{exp:user:getname}
{exp:user:getemail}
This works fine, however it seems to create multiple instances of the User class. If I was doing some memory intensive stuff in my constructor this would be a massive waste of resources.
Is it possible to declare your class, and then separately access the methods from within that instance of the class… ie:
{exp:user}
{getname}
{getemail}
{/exp:user}
Sorry if this is a really stupid/simple question.. I’ve tried the second approach and I can’t get it to work.
Mod Edit: Moved to Plugins: Technical Assistance
You could do something like that, yes. For example:
class User
{
var $return_data;
function User()
{
global $TMPL;
foreach($TMPL->var_single AS $method)
{
if(method_exists($this, $method))
{
$TMPL->tagdata = $TMPL->swap_var_single($method, $this->$method(), $TMPL->tagdata);
}
}
return $this->return_data = $TMPL->tagdata;
}
function getname()
{
return "John Doe";
}
function getemail()
{
return "[email protected]";
}
}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.