class Template{
private $_tpldata = array();
public $files = array();
public $root = '';
private $compiled_code = array();
private $uncompiled_code = array();
private $use_cache = true;
private $cache_directory = '';
function __construct($root = "." , $use_cache = false, $cache_directory = '')
{
if($this->set_rootdir($root))
{
} else {
print('Unable to find template root directory: ' . $root);
}
$this->use_cache = $use_cache;
if($this->use_cache)
{
if($cache_directory)
{
$this->cache_directory = $cache_directory;
} else {
$this->cache_directory = $this->root . '/cache';
}
}
}
function __destruct()
{
$this->_tpldata = array();
}
public function set_rootdir($dir)
{
if(!is_dir($dir))
{
return false;
}
$this->root = $dir;
return true;
}
public function set_filenames(array $filename_array)
{
reset($filename_array);
foreach($filename_array as $handle => $filename)
{
$this->files[$handle] = $this->make_filename($filename);
}
return true;
}
public function pparse($handle)
{
$code = '';
if($this->use_cache)
{
$code = $this->get_cached_code($handle);
} else {
if(!$this->loadfile($handle))
{
print("Template->pparse(): Couldn't load template file for handle $handle");
}
if(!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle]))
{
$this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]);
}
$code = $this->compiled_code[$handle];
}
eval($code);
return true;
}
public function get_html($handle)
{
if(!$this->loadfile($handle))
{
print("Template->assign_var_from_handle(): Couldn't load template file for handle $handle");
}
$_str = "";
$code = '';
if($this->use_cache)
{
$code = $this->get_cached_code($handle, true, '_str');
} else {
$code = $this->compile($this->uncompiled_code[$handle], true, '_str');
}
eval($code);
return $_str;
}
public function assign_var_from_handle($varname, $handle)
{
if(!$this->loadfile($handle))
{
print("Template->assign_var_from_handle(): Couldn't load template file for handle $handle");
}
$_str = "";
$code = '';
if($this->use_cache)
{
$code = $this->get_cached_code($handle, true, '_str');
} else {
$code = $this->compile($this->uncompiled_code[$handle], true, '_str');
}
eval($code);
$this->assign_var($varname, $_str);
return true;
}
public function assign_block_vars($blockname, $vararray)
{
if(strstr($blockname, '.'))
{
$blocks = explode('.', $blockname);
$blockcount = sizeof($blocks) - 1;
$str = '$this->_tpldata';
for ($i = 0; $i < $blockcount; $i++)
{
$str .= '[\'' . $blocks[$i] . '.\']';
eval('$lastiteration = sizeof(' . $str . ') - 1;');
$str .= '[' . $lastiteration . ']';
}
$str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;';
eval($str);
}
else
{
$this->_tpldata[$blockname . '.'][] = $vararray;
}
return true;
}
public function assign_vars($vararray)
{
reset ($vararray);
while (list($key, $val) = each($vararray))
{
$this->_tpldata['.'][0][$key] = $val;
}
return true;
}
public function assign_var($varname, $varval)
{
$this->_tpldata['.'][0][$varname] = $varval;
return true;
}
private function make_filename($filename)
{
$from_http = 0;
if(strtolower(substr($filename, 0 ,4)) == 'http')
{
$from_http = 1;
}
if($from_http)
{
} else {
if(substr($filename, 0, 1) != '/')
{
$filename = realpath($this->root . '/' . $filename);
}
if(!file_exists($filename))
{
print("Template->make_filename(): Error - file $filename does not exist");
}
}
return $filename;
}
private function loadfile($handle)
{
if(isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle]))
{
return true;
}
if(!isset($this->files[$handle]))
{
print("Template->loadfile(): No file specified for handle $handle");
}
$filename = $this->files[$handle];
$str = implode("", @file($filename));
if(empty($str))
{
print("Template->loadfile(): File $filename for handle $handle is empty");
}
$this->uncompiled_code[$handle] = $str;
return true;
}
public function compile($code, $do_not_echo = false, $retvar = '')
{
$code = str_replace('\\', '\\\\', $code);
$code = str_replace('\'', '\\\'', $code);
$varrefs = array();
preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)([a-z0-9\-_]+?)\}#is', $code, $varrefs);
$varcount = sizeof($varrefs[1]);
for ($i = 0; $i < $varcount; $i++)
{
$namespace = $varrefs[1][$i];
$varname = $varrefs[3][$i];
$new = $this->generate_block_varref($namespace, $varname);
$code = str_replace($varrefs[0][$i], $new, $code);
}
$code = preg_replace('#\{([a-z0-9\-_]*?)\}#is', '\' . ((isset($this->_tpldata[\'.\'][0][\'\1\'])) ? $this->_tpldata[\'.\'][0][\'\1\'] : \'\') . \'', $code);
$code_lines = explode("\n", $code);
$block_nesting_level = 0;
$block_names = array();
$block_names[0] = ".";
$line_count = sizeof($code_lines);
for ($i = 0; $i < $line_count; $i++)
{
$code_lines[$i] = chop($code_lines[$i]);
if(preg_match('#<!-- BEGIN (.*?) -->#', $code_lines[$i], $m))
{
$n[0] = $m[0];
$n[1] = $m[1];
if(preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $n))
{
$block_nesting_level++;
$block_names[$block_nesting_level] = $m[1];
if($block_nesting_level < 2)
{
$code_lines[$i] = '$_' . $n[1] . '_count = (isset($this->_tpldata[\'' . $n[1] . '.\'])) ? sizeof($this->_tpldata[\'' . $n[1] . '.\']) : 0;';
$code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)';
$code_lines[$i] .= "\n" . '{';
}
else
{
$namespace = implode('.', $block_names);
$namespace = substr($namespace, 2);
$varref = $this->generate_block_data_ref($namespace, false);
$code_lines[$i] = '$_' . $n[1] . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
$code_lines[$i] .= "\n" . 'for ($_' . $n[1] . '_i = 0; $_' . $n[1] . '_i < $_' . $n[1] . '_count; $_' . $n[1] . '_i++)';
$code_lines[$i] .= "\n" . '{';
}
unset($block_names[$block_nesting_level]);
$block_nesting_level--;
$code_lines[$i] .= '} // END ' . $n[1];
$m[0] = $n[0];
$m[1] = $n[1];
}
else
{
$block_nesting_level++;
$block_names[$block_nesting_level] = $m[1];
if($block_nesting_level < 2)
{
$code_lines[$i] = '$_' . $m[1] . '_count = (isset($this->_tpldata[\'' . $m[1] . '.\'])) ? sizeof($this->_tpldata[\'' . $m[1] . '.\']) : 0;';
$code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)';
$code_lines[$i] .= "\n" . '{';
}
else
{
$namespace = implode('.', $block_names);
$namespace = substr($namespace, 2);
$varref = $this->generate_block_data_ref($namespace, false);
$code_lines[$i] = '$_' . $m[1] . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
$code_lines[$i] .= "\n" . 'for ($_' . $m[1] . '_i = 0; $_' . $m[1] . '_i < $_' . $m[1] . '_count; $_' . $m[1] . '_i++)';
$code_lines[$i] .= "\n" . '{';
}
}
}
else if(preg_match('#<!-- END (.*?) -->#', $code_lines[$i], $m))
{
unset($block_names[$block_nesting_level]);
$block_nesting_level--;
$code_lines[$i] = '} // END ' . $m[1];
}
else
{
if(!$do_not_echo)
{
$code_lines[$i] = 'echo \'' . $code_lines[$i] . '\' . "\\n";';
}
else
{
$code_lines[$i] = '$' . $retvar . '.= \'' . $code_lines[$i] . '\' . "\\n";';
}
}
}
$code = implode("\n", $code_lines);
return $code ;
}
private function generate_block_varref($namespace, $varname)
{
$namespace = substr($namespace, 0, strlen($namespace) - 1);
$varref = $this->generate_block_data_ref($namespace, true);
$varref .= '[\'' . $varname . '\']';
$varref = '\' . ((isset(' . $varref . ')) ? ' . $varref . ' : \'\') . \'';
return $varref;
}
private function generate_block_data_ref($blockname, $include_last_iterator)
{
$blocks = explode(".", $blockname);
$blockcount = sizeof($blocks) - 1;
$varref = '$this->_tpldata';
for ($i = 0; $i < $blockcount; $i++)
{
$varref .= '[\'' . $blocks[$i] . '.\'][$_' . $blocks[$i] . '_i]';
}
$varref .= '[\'' . $blocks[$blockcount] . '.\']';
if($include_last_iterator)
{
$varref .= '[$_' . $blocks[$blockcount] . '_i]';
}
return $varref;
}
private function get_cached_code($handle , $do_not_echo = false, $ret_var = '')
{
$cache_file = $this->cache_directory . '/' . md5($this->files[$handle]);
$create_new_cache = 1;
if($cache_present = file_exists($cache_file))
{
$cache_stat = stat($cache_file);
$template_stat = stat($this->files[$handle]);
$create_new_cache = ($cache_stat['mtime'] < $template_stat['mtime'])?1:0 ;
}
$code = '';
if($create_new_cache)
{
if(!$this->loadfile($handle))
{
print("Template->pparse(): Couldn't load template file for handle $handle");
}
if(!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle]))
{
$code = $this->compile($this->uncompiled_code[$handle], $do_not_echo, $ret_var);
$this->compiled_code[$handle] = $code;
}
$fh = fopen($cache_file, 'w');
fwrite($fh, $code);
fclose($fh);
} else {
$fh = fopen($cache_file, 'r');
$code = fread($fh, filesize($cache_file));
fclose($fh);
}
return $code;
}
}