If in ForEach now possible in templates

feature/node-rewrite
Sven Slootweg 13 years ago
parent 57203f33b0
commit 937059ac52

@ -82,18 +82,42 @@ 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);
$this->tpl_rendered = $this->ParseIf($this->tpl_rendered, $strings);
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
foreach($strlist[1] as $str)
{
if(isset($strings[$str]))
{
$this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered);
}
}
}
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]; $variable_name = $matches[1];
$array_name = $matches[2]; $array_name = $matches[2];
$template = $matches[3]; $template = $matches[3];
$returnvalue = ""; $returnvalue = "";
if(isset($strings[$array_name])) if(isset($data[$array_name]))
{ {
foreach($strings[$array_name] as $item) foreach($data[$array_name] as $item)
{ {
$rendered = $template; $rendered = $template;
$rendered = $templater->ParseIf($rendered, $data, $item, $variable_name);
foreach($item as $key => $value) foreach($item as $key => $value)
{ {
$rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered); $rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered);
@ -106,17 +130,62 @@ class Templater
} }
return false; return false;
}, $this->tpl_rendered); }, $source);
}
$this->tpl_rendered = preg_replace_callback("/<%if ([a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($strings) { public function ParseIf($source, $data, $context = null, $identifier = "")
{
return preg_replace_callback("/<%if ([][a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($data, $context, $identifier) {
$variable_name = $matches[1]; $variable_name = $matches[1];
$operator = $matches[2]; $operator = $matches[2];
$value = $matches[3]; $value = $matches[3];
$template = $matches[4]; $template = $matches[4];
if(isset($strings[$variable_name])) if(!empty($identifier))
{
if(preg_match("/{$identifier}\[([a-z0-9_-]+)\]/i", $variable_name, $submatches))
{ {
$variable = $strings[$variable_name]; // Local variable.
$name = $submatches[1];
if(isset($context[$name]))
{
$variable = $context[$name];
}
else
{
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
{
return false;
}
}
}
else
{
if(isset($data[$variable_name]))
{
$variable = $data[$variable_name];
}
else
{
return false;
}
}
if($variable == "true") { $variable = true; } if($variable == "true") { $variable = true; }
if($variable == "false") { $variable = false; } if($variable == "false") { $variable = false; }
@ -159,24 +228,9 @@ class Templater
{ {
return ""; return "";
} }
}
return false; return false;
}, $this->tpl_rendered); }, $source);
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
foreach($strlist[1] as $str)
{
if(isset($strings[$str]))
{
$this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered);
}
}
}
else
{
Throw new Exception("No template loaded.");
}
} }
public function Render() public function Render()

@ -11,7 +11,17 @@
<%foreach container in containers> <%foreach container in containers>
<tr> <tr>
<td><%?container[hostname]></td> <td><%?container[hostname]></td>
<td><%?container[virtualization-type]></td> <td>
<%if container[virtualization-type] == 1>
OpenVZ
<%/if><%if container[virtualization-type] == 2>
Xen PV
<%/if><%if container[virtualization-type] == 3>
Xen HVM
<%/if><%if container[virtualization-type] == 4>
KVM
<%/if>
</td>
<td><%?container[node]> (<%?container[node-hostname]>)</td> <td><%?container[node]> (<%?container[node-hostname]>)</td>
<td><%?container[diskspace]></td> <td><%?container[diskspace]></td>
<td><%?container[guaranteed-ram]></td> <td><%?container[guaranteed-ram]></td>

Loading…
Cancel
Save