|
|
@ -82,101 +82,155 @@ class Templater
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(!is_null($this->tpl))
|
|
|
|
if(!is_null($this->tpl))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$this->tpl_rendered = preg_replace_callback("/<%foreach ([a-z0-9_-]+) in ([a-z0-9_-]+)>(.*?)<%\/foreach>/si", function($matches) use($strings) {
|
|
|
|
$this->tpl_rendered = $this->ParseForEach($this->tpl_rendered, $strings);
|
|
|
|
$variable_name = $matches[1];
|
|
|
|
$this->tpl_rendered = $this->ParseIf($this->tpl_rendered, $strings);
|
|
|
|
$array_name = $matches[2];
|
|
|
|
|
|
|
|
$template = $matches[3];
|
|
|
|
|
|
|
|
$returnvalue = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($strings[$array_name]))
|
|
|
|
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
|
|
|
|
|
|
|
|
foreach($strlist[1] as $str)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(isset($strings[$str]))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach($strings[$array_name] as $item)
|
|
|
|
$this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
$rendered = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Throw new Exception("No template loaded.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function ParseForEach($source, $data)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$templater = $this;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return preg_replace_callback("/<%foreach ([a-z0-9_-]+) in ([a-z0-9_-]+)>(.*?)<%\/foreach>/si", function($matches) use($data, $templater) {
|
|
|
|
|
|
|
|
$variable_name = $matches[1];
|
|
|
|
|
|
|
|
$array_name = $matches[2];
|
|
|
|
|
|
|
|
$template = $matches[3];
|
|
|
|
|
|
|
|
$returnvalue = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($data[$array_name]))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach($data[$array_name] as $item)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$rendered = $template;
|
|
|
|
|
|
|
|
|
|
|
|
foreach($item as $key => $value)
|
|
|
|
$rendered = $templater->ParseIf($rendered, $data, $item, $variable_name);
|
|
|
|
{
|
|
|
|
|
|
|
|
$rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$returnvalue .= $rendered;
|
|
|
|
foreach($item as $key => $value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $returnvalue;
|
|
|
|
$returnvalue .= $rendered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return $returnvalue;
|
|
|
|
}, $this->tpl_rendered);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->tpl_rendered = preg_replace_callback("/<%if ([a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($strings) {
|
|
|
|
return false;
|
|
|
|
$variable_name = $matches[1];
|
|
|
|
}, $source);
|
|
|
|
$operator = $matches[2];
|
|
|
|
}
|
|
|
|
$value = $matches[3];
|
|
|
|
|
|
|
|
$template = $matches[4];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($strings[$variable_name]))
|
|
|
|
public function ParseIf($source, $data, $context = null, $identifier = "")
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$variable = $strings[$variable_name];
|
|
|
|
return preg_replace_callback("/<%if ([][a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($data, $context, $identifier) {
|
|
|
|
|
|
|
|
$variable_name = $matches[1];
|
|
|
|
|
|
|
|
$operator = $matches[2];
|
|
|
|
|
|
|
|
$value = $matches[3];
|
|
|
|
|
|
|
|
$template = $matches[4];
|
|
|
|
|
|
|
|
|
|
|
|
if($variable == "true") { $variable = true; }
|
|
|
|
if(!empty($identifier))
|
|
|
|
if($variable == "false") { $variable = false; }
|
|
|
|
{
|
|
|
|
if(is_numeric($variable)) { $variable = (int)$variable; }
|
|
|
|
if(preg_match("/{$identifier}\[([a-z0-9_-]+)\]/i", $variable_name, $submatches))
|
|
|
|
if($value == "true") { $value = true; }
|
|
|
|
{
|
|
|
|
if($value == "false") { $value = false; }
|
|
|
|
// Local variable.
|
|
|
|
if(is_numeric($value)) { $value = (int)$value; }
|
|
|
|
$name = $submatches[1];
|
|
|
|
|
|
|
|
|
|
|
|
switch($operator)
|
|
|
|
if(isset($context[$name]))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case "=":
|
|
|
|
$variable = $context[$name];
|
|
|
|
case "==":
|
|
|
|
|
|
|
|
$display = ($variable == $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ">":
|
|
|
|
|
|
|
|
$display = ($variable > $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "<":
|
|
|
|
|
|
|
|
$display = ($variable < $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ">=":
|
|
|
|
|
|
|
|
$display = ($variable >= $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "<=":
|
|
|
|
|
|
|
|
$display = ($variable <= $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "!=":
|
|
|
|
|
|
|
|
$display = ($variable != $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
if($display === true)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return $template;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elseif(preg_match("/[a-z0-9_-]+\[[a-z0-9_-]+\]/i", $variable_name))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Not the right scope.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Global variable.
|
|
|
|
|
|
|
|
if(isset($data[$variable_name]))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$variable = $data[$variable_name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
}, $this->tpl_rendered);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
|
|
|
|
|
|
|
|
foreach($strlist[1] as $str)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(isset($strings[$str]))
|
|
|
|
if(isset($data[$variable_name]))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered);
|
|
|
|
$variable = $data[$variable_name];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
if($variable == "true") { $variable = true; }
|
|
|
|
{
|
|
|
|
if($variable == "false") { $variable = false; }
|
|
|
|
Throw new Exception("No template loaded.");
|
|
|
|
if(is_numeric($variable)) { $variable = (int)$variable; }
|
|
|
|
}
|
|
|
|
if($value == "true") { $value = true; }
|
|
|
|
|
|
|
|
if($value == "false") { $value = false; }
|
|
|
|
|
|
|
|
if(is_numeric($value)) { $value = (int)$value; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch($operator)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case "=":
|
|
|
|
|
|
|
|
case "==":
|
|
|
|
|
|
|
|
$display = ($variable == $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ">":
|
|
|
|
|
|
|
|
$display = ($variable > $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "<":
|
|
|
|
|
|
|
|
$display = ($variable < $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ">=":
|
|
|
|
|
|
|
|
$display = ($variable >= $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "<=":
|
|
|
|
|
|
|
|
$display = ($variable <= $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "!=":
|
|
|
|
|
|
|
|
$display = ($variable != $value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($display === true)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}, $source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function Render()
|
|
|
|
public function Render()
|
|
|
|