Implement variable hooks

develop
Sven Slootweg 12 years ago
parent c7ee85eab1
commit 52497d05b4

@ -15,6 +15,7 @@ if($_CPHP !== true) { die(); }
$template_cache = array(); $template_cache = array();
$template_global_vars = array(); $template_global_vars = array();
$template_variable_hooks = array();
define("MODE_NONE", 0); define("MODE_NONE", 0);
define("MODE_TAG", 1); define("MODE_TAG", 1);
@ -444,6 +445,14 @@ class NewTemplater
{ {
return $this->root->Evaluate($localized_strings, $data); return $this->root->Evaluate($localized_strings, $data);
} }
public static function RegisterVariableHook($name, $function)
{
global $template_variable_hooks;
$template_variable_hooks[$name] = $function;
return true;
}
} }
class TemplateElement class TemplateElement
@ -491,6 +500,8 @@ class TemplateElement
public function FetchVariable($variable_name, $data) public function FetchVariable($variable_name, $data)
{ {
global $template_variable_hooks;
$operation = null; $operation = null;
if(strpos($variable_name, "|") !== false) if(strpos($variable_name, "|") !== false)
@ -511,6 +522,26 @@ class TemplateElement
$target = $this->parent; $target = $this->parent;
if(isset($template_variable_hooks[$collection_name]))
{
$function = $template_variable_hooks[$collection_name];
if(is_null($operation))
{
return $function(true, $key_name);
}
elseif($operation == "isset")
{
return true;
}
elseif($operation == "isempty")
{
$result = $function(false, $key_name);
return empty($result);
}
}
else
{
/* Traverse up the tree to find the provider of this particular collection */ /* Traverse up the tree to find the provider of this particular collection */
while(true) while(true)
{ {
@ -555,12 +586,31 @@ class TemplateElement
} }
} }
} }
}
else else
{ {
/* Stand-alone variable. */ /* Stand-alone variable. */
$target = $this->parent; $target = $this->parent;
if(isset($data[$variable_name])) if(isset($template_variable_hooks[$variable_name]))
{
$function = $template_variable_hooks[$variable_name];
if(is_null($operation))
{
return $function(true);
}
elseif($operation == "isset")
{
return true;
}
elseif($operation == "isempty")
{
$result = $function(false);
return empty($result);
}
}
elseif(isset($data[$variable_name]))
{ {
if(is_null($operation)) if(is_null($operation))
{ {

Loading…
Cancel
Save