<?php
class Btemplate
{
var $base_path;
var $reset_vars;
var $ldelim = '<';
var $rdelim = ' />';
var $BAldelim = '<';
var $BArdelim = '>';
var $EAldelim = '</';
var $EArdelim = '>';
var $scalars = array();
var $arrays = array();
var $carrays = array();
var $ifs = array();
function Btemplate($base_path = NULL, $reset_vars = TRUE) {
if ($base_path) {
$this->base_path = $base_path;
}
$this->reset_vars = $reset_vars;
}
function set($tag, $var, $if = NULL) {
if (is_array($var)) {
$this->arrays[$tag] = $var;
if ($if) {
$result = $var ? TRUE : FALSE;
$this->ifs[] = $tag;
$this->scalars[$tag] = $result;
}
} else {
$this->scalars[$tag] = $var;
if ($if) {
$this->ifs[] = $tag;
}
}
}
function set_cloop($tag, $array, $cases) {
$this->carrays[$tag] = array(
'array' => $array,
'cases' => $cases);
}
function reset_vars($scalars, $arrays, $carrays, $ifs) {
if ($scalars==true) { $this->scalars = array(); }
if ($arrays==true) { $this->arrays = array(); }
if ($carrays==true) { $this->carrays = array(); }
if ($ifs==true) { $this->ifs = array(); }
}
function get_tags($tag, $directive) {
$tags['b'] = $this->BAldelim . $directive . $tag . $this->BArdelim;
$tags['e'] = $this->EAldelim . $directive . $tag . $this->EArdelim;
return $tags;
}
function get_tag($tag) {
return $this->ldelim . 'tag:' . $tag . $this->rdelim;
}
function get_statement($t, &$contents) {
$tag_length = strlen($t['b']);
$tags = array();
$fpos = strpos($contents, $t['b']) + $tag_length;
while ($fpos) {
$lpos = strpos($contents, $t['e'], $fpos);
$length = $lpos - $fpos;
$tags[] = substr($contents, $fpos, $length);
$fpos = strpos($contents, $t['b'], $lpos);
}
return $tags;
}
function parse(&$contents) {
if (!empty($this->ifs)) {
foreach($this->ifs as $value) {
$contents = $this->parse_if($value, $contents);
}
}
foreach($this->scalars as $key => $value) {
$contents = str_replace($this->get_tag($key), $value, $contents);
}
foreach($this->arrays as $key => $array) {
$contents = $this->parse_loop($key, $array, $contents);
}
foreach($this->carrays as $key => $array) {
$contents = $this->parse_cloop($key, $array, $contents);
}
if ($this->reset_vars) {
$this->reset_vars(FALSE, TRUE, TRUE, TRUE);
}
return $contents;
}
function parse_if($tag, &$contents) {
$t = $this->get_tags($tag, 'if:');
$entire_statement = $this->get_statement($t, $contents);
$num_statements = count($entire_statement);
for($x = 0; $x < $num_statements; $x++) {
$else_tags = array(
'b' => NULL,
'e' => $this->BAldelim . 'else:' . $tag . $this->BArdelim
);
$else_pos = strpos($entire_statement[$x], $else_tags['e']);
if ($else_pos) {
$if = substr($entire_statement[$x], 0, $else_pos);
$else = substr($entire_statement[$x], $else_pos + strlen($else_tags['e']));
} else {
$else = NULL;
$if = $entire_statement[$x];
}
$this->scalars[$tag] ? $replace = $if : $replace = $else;
$contents = str_replace($entire_statement[$x], $replace, $contents);
}
return $contents;
}
function parse_loop($tag, $array, $contents) {
$t = $this->get_tags($tag, 'loop:');
$loop = $this->get_statement($t, $contents);
$parsed = NULL;
foreach($array as $key => $value) {
if (is_numeric($key) && is_array($value)) {
$i = $loop;
foreach($value as $key2 => $value2) {
if (!is_array($value2)) {
$i = str_replace($this->get_tag($tag . '[].' . $key2), $value2, $i);
} else {
$i = $this->parse_loop($tag . '[].' . $key2, $value2, $i);
}
}
} elseif (is_string($key) && !is_array($value)) {
$contents = str_replace($this->get_tag($tag . '.' . $key), $value, $contents);
} elseif (!is_array($value)) {
$i = str_replace($this->get_tag($tag . '[]'), $value, $loop);
}
if(isset($i)) {
$parsed .= rtrim($i);
}
}
return str_replace($t['b'] . $loop . $t['e'], $parsed, $contents);
}
function parse_cloop($tag, $array, $contents) {
$t = $this->get_tags($tag, 'cloop:');
$loop = $this->get_statement($t, $contents);
$array['cases'][] = 'default';
$case_content = array();
$parsed = NULL;
foreach($array['cases'] as $case) {
$ctags[$case] = $this->get_tags($case, 'case:');
$case_content[$case] = $this->get_statement($ctags[$case], $loop);
}
foreach($array['array'] as $key => $value) {
if (is_numeric($key) && is_array($value)) {
if (isset($value['case'])) {
$current_case = $value['case'];
} else {
$current_case = 'default';
}
unset($value['case']);
$i = $case_content[$current_case];
foreach($value as $key2 => $value2) {
$i = str_replace($this->get_tag($tag . '[].' . $key2), $value2, $i);
}
}
$parsed .= rtrim($i);
}
return str_replace($t['b'] . $loop . $t['e'], $parsed, $contents);
}
function fetch($file_name) {
$file = $this->base_path . $file_name;
$fp = fopen($file, 'rb');
if (!$fp) {
return FALSE;
}
if(filesize($file) > 0) {
$contents = fread($fp, filesize($file));
} else {
return $file_name . ' is empty.';
}
fclose($fp);
return $this->parse($contents);
}
}
?>