class SimpleTemplate
{
private $templateCode;
private $templateHtml;
const COMMENT = '/<!-- #(.+):(.+)\s+--(>)/iU';
public function __construct($html = '', $code = '')
{
$this->templateCode = $code;
$this->templateHtml = $html;
}
public function __set($prop, $value)
{
switch ($prop)
{
case 'Html':
$this->templateHtml = $value;
break;
case 'Code':
$this->templateCode = $value;
break;
}
}
public function __get($prop)
{
switch ($prop)
{
case 'Html':
return $this->templateHtml;
case 'Code':
return $this->templateCode;
}
}
public function RunTemplate()
{
if (strlen($this->templateHtml) > 0 && file_exists($this->templateHtml))
{
// Template Laden
$html = file_get_contents($this->templateHtml);
}
else
{
die ('Fehler: Vorlage konnte nicht gefunden werden. ');
}
// Wiederholungen aufdroeseln
$matches = array();
$blocksFound = preg_match_all(self::COMMENT, $html, $matches, PREG_OFFSET_CAPTURE);
if ($blocksFound)
{
$page = '';
// var_export($matches);
// 0: Array der Kommentaranfaenge
// 1: TAGs
// 2: Variablen
// 3: Ende-Offset
if (is_array($matches))
{
$startBlock = $lastStartBlock = 0;
for ($i = 0; $i < count($matches[1]); $i++)
{
$token = strtoupper($matches[1][$i][0]);
$offsetComment = (int)$matches[0][$i][1];
$offsetContent = ((int)$matches[3][$i][1]) + 1;
$variableName = $matches[2][$i][0];
switch ($token)
{
case 'REPEAT':
$startBlock = $offsetContent;
$startComment = $offsetComment;
break;
case 'ENDREPEAT':
$block = substr($html, $startBlock, $offsetComment - $startBlock);
$block = $this->repeatBlock($block, $variableName);
$len = $startComment - $lastStartBlock;
$page .= substr($html, $lastStartBlock, $len).$block;
$lastStartBlock = $offsetContent;
break;
case 'IF':
// Vor dem IF den Rest der Seite vom letzten Code ausgeben
$page .= substr($html, $lastStartBlock, $offsetComment - $lastStartBlock);
$startBlock = $offsetContent;
$startComment = $offsetComment;
break;
case 'ENDIF':
global $$variableName;
$len = $startComment - $lastStartBlock;
$lastStartBlock = $offsetContent;
if (isset($$variableName))
{
// substr($html, $lastStartBlock, $len).
$page .= substr($html, $startBlock, $offsetComment - $startBlock);
}
break;
}
}
// Rest der Seite
$page .= substr($html, $offsetContent);
}
}
else
{
// Wenn kein Code enthalten
$page = $html;
}
echo ($this->varReplace($page));
}
private function repeatBlock($block, $varName)
{
global $$varName;
$result = '';
if (is_array($$varName))
{
$this->index = 0;
if (count($$varName) > 0)
{
foreach ($$varName as $value)
{
$result .= $this->arrayReplace($block);
$this->index++;
}
}
}
return $result;
}
private $index = 0;
private function replaceArray($repl)
{
global $$repl[1];
$arr = $$repl[1];
return $arr[$this->index][$repl[2]];
}
private function replaceSkalar($repl)
{
global $$repl[1];
return $$repl[1];
}
private function arrayReplace($block)
{
$block = preg_replace_callback('/\{\$([^:}]*):(.+)\}/U', array($this, 'replaceArray'), $block);
return $block;
}
private function varReplace($block)
{
$block = preg_replace_callback('/\{\$(.+)\}/U', array($this, 'replaceSkalar'), $block);
return $block;
}
}
$__template = isset($_GET['TEMPLATE']) ? $_GET['TEMPLATE'] : '';
return new SimpleTemplate($__template);