<?php
class template{
var $dat;
function open($path)
{
if(!file_exists($path))
{
trigger_error("file: ".$path." does not exist");
}
$this->dat=file_get_contents($path);
}
function parse()
{
$this->dat=str_replace("{\$_self}",$_SERVER['PHP_SELF'],$this->dat);
$this->dat=str_replace("{\$_sess}",session_id(),$this->dat);
$this->dat=preg_replace_callback("/\{\\\$([a-z_][a-z0-9_]*)(?:\[([a-z0-9_-]*)\])?(?:\[([a-z0-9_-]*)\])?\}/",array($this,"variable"),$this->dat);
$this->dat=preg_replace_callback("/\{if\|(.+?(?:==|>|<|>=|<=).+?)\}(.+?)\{endif\}/s",array($this,"condition"),$this->dat);
}
function condition($var)
{
$var[2]=str_replace("\\'","'",$var[2]);
if(strpos($var[2],"{else}")!==false)
{
$buf=explode("{else}",$var[2]);
}
eval("\$tmp=".$var[1].";");
if($tmp===true)
{
return isset($buf) ? $buf[0] : $var[2];
}
return isset($buf) ? $buf[1] : "";
}
function variable($var)
{
if($var[2]=="")
{
if(isset($GLOBALS[$var[1]]))
{
return $GLOBALS[$var[1]];
}
else
{
return "parse error";
}
}
if($var[3]=="")
{
if(isset($GLOBALS[$var[1]][$var[2]]))
{
return $GLOBALS[$var[1]][$var[2]];
}
else
{
return "parse error";
}
}
if(isset($GLOBALS[$var[1]][$var[2]][$var[3]]))
{
return $GLOBALS[$var[1]][$var[2]][$var[3]];
}
return "parse error";
}
}
?>