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_global_vars = array();
$template_variable_hooks = array();
define("MODE_NONE", 0);
define("MODE_TAG", 1);
@ -444,6 +445,14 @@ class NewTemplater
{
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
@ -491,6 +500,8 @@ class TemplateElement
public function FetchVariable($variable_name, $data)
{
global $template_variable_hooks;
$operation = null;
if(strpos($variable_name, "|") !== false)
@ -511,6 +522,26 @@ class TemplateElement
$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 */
while(true)
{
@ -555,12 +586,31 @@ class TemplateElement
}
}
}
}
else
{
/* Stand-alone variable. */
$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))
{

Loading…
Cancel
Save