null,
'method_function_name' => '_HTML_Template_Platon_function_',
'method_function_idx' => 0
);
} /* }}} */
// Output method (bit array) {{{
define('HTML_TEMPLATE_PLATON_METHOD_NORMAL', 0);
define('HTML_TEMPLATE_PLATON_METHOD_FUNCTION', 1);
define('HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP', 2);
// }}}
// Set method (enumeration constants) {{{
define('HTML_TEMPLATE_PLATON_SET_VALUE', 0);
define('HTML_TEMPLATE_PLATON_SET_CODE', 1);
define('HTML_TEMPLATE_PLATON_SET_PARSE', 2);
define('HTML_TEMPLATE_PLATON_SET_VAR', 3);
define('HTML_TEMPLATE_PLATON_SET_ALIAS', 3); // the same as old SET_VAR
// }}}
class HTML_Template_Platon
{
var $_cstr = array(
'begin' => '',
'classname' => 'HTML_Template_Platon',
'version' => '0.5'
);
var $_tag = array(
'begin' => '<|',
'command' => 'cmd|',
'file' => 'include|',
'start' => 'start',
'stop' => 'stop',
'end' => '|>'
);
var $_array = array();
var $_vars = array();
var $_set_vars = null;
var $_method = null;
var $_filename = null;
var $_load_fnc = null;
var $_debug = 1;
var $_prepended = 0;
function __construct($filename = '', /* ...) {{{ */
$method = HTML_TEMPLATE_PLATON_METHOD_NORMAL)
{
global $_HTML_Template_Platon_cfg;
foreach (array_keys($this->_tag) as $key) {
$this->_tag[$key] = preg_quote($this->_tag[$key]);
$this->_tag[$key.'_striped'] = stripslashes($this->_tag[$key]);
}
$this->_method = $method;
$this->_filename = $filename;
$this->_load_fnc = $_HTML_Template_Platon_cfg['load_function'];
$this->reload();
return true;
} /* }}} */
function setTags($begin_tag = '', $end_tag = '') /* {{{ */
{
if (strlen($begin_tag) > 0) {
$this->_tag['begin'] = $begin_tag;
}
if (strlen($end_tag) > 0) {
$this->_tag['end'] = $end_tag;
}
} /* }}} */
function setMethod($method) /* {{{ */
{
$this->_method = $method;
} /* }}} */
static function setLoadFunction($function_name) /* {{{ */
{
if (isset($this)) {
$this->_load_fnc = $function_name;
} else {
global $_HTML_Template_Platon_cfg;
$_HTML_Template_Platon_cfg['load_function'] = $function_name;
}
} /* }}} */
function _load($filename) /* {{{ */
{
if (isset($this->_load_fnc)) {
$load_fnc = $this->_load_fnc;
$data = $load_fnc($filename);
} else {
$data = '';
if ($f = fopen($filename, 'r')) {
while ($tmp_data = fread($f, 1024)) {
$data .= $tmp_data;
}
fclose($f);
}
}
if (! isset($data) || $data === false) {
return array('HTML_Template_Platon ERROR: cannot load template with key '."'$filename'");
}
return $this->_stringTransform($data);
} /* }}} */
function load($filename) /* {{{ */
{
$this->_filename = $filename;
$this->_array = $this->_load($filename);
return true;
} /* }}} */
function reload() /* {{{ */
{
$this->_set_vars = array();
if (count($this->_array) > 0) {
if ($this->_prepended > 0)
$this->_array = array_slice($this->_array, $this->_prepended);
$this->_prepended = 0;
return true;
}
$this->_array = $this->_load($this->_filename);
return $this->_array !== false;
} /* }}} */
function getArray() /* {{{ */
{
return $this->_array;
} /* }}} */
function getVars() /* {{{ */
{
$this->_vars = array_unique($this->_vars);
return $this->_vars;
} /* }}} */
function isVar($var) /* {{{ */
{
return in_array($var, $this->_vars);
} /* }}} */
function _stringTransform($string) /* {{{ */
{
// TODO: this regexp needs to be improved
$string = str_replace($this->_tag['end_striped'], $this->_tag['end_striped'] . "\n", $string);
$regexp = $this->_tag['begin'] . '(.+)' . $this->_tag['end'] . '[\n]';
$array = preg_split("/$regexp/im", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 1; $i < count($array); $i += 2) {
preg_match_all('/\$(\w[\w\d_]*)/', $array[$i], $matches);
$this->_vars = array_merge($this->_vars, $matches[1]);
if (! strncasecmp($array[$i], $this->_tag['command_striped'],
strlen($this->_tag['command_striped']))) {
$array[$i] = substr($array[$i], strlen($this->_tag['command_striped']));
} elseif (! strncasecmp($array[$i], $this->_tag['file_striped'],
strlen($this->_tag['file_striped']))) {
$filename = dirname($this->_filename) . '/'
. trim(substr($array[$i], strlen($this->_tag['file_striped'])));
$subarray = $this->_load($filename);
array_push($subarray, '');
array_unshift($subarray, '');
$array = array_merge(array_slice($array, 0, $i), $subarray, array_slice($array, $i + 1));
$i += count($subarray) - 1;
} else {
$array[$i] = 'echo(' . $array[$i] . ');';
}
}
return $array;
} /* }}} */
function setParse($key, $value = '') /* {{{ */
{
return $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_PARSE);
} /* }}} */
function setCode($key, $value = '') /* {{{ */
{
return $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_CODE);
} /* }}} */
function setVar($key, $value = '') /* {{{ */
{
return $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_VAR);
} /* }}} */
function setValue($key, $value = '') /* {{{ */
{
return $this->set($key, $value);
} /* }}} */
function setAlias($key, $value) /* {{{ */
{
return $this->setVar($key, $value);
} /* }}} */
function set($key, $value = '', /* ...) {{{ */
$type = HTML_TEMPLATE_PLATON_SET_VALUE)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
if (! empty($k)) {
// Empty values are allowed. Empty keys are NOT.
$this->set($k, $v, $type);
}
}
} else if (is_array($value)) {
die(sprintf('HTML_Template_Platon ERROR: $value in set(%s) is an array', $key));
} else {
if ($type == HTML_TEMPLATE_PLATON_SET_CODE) {
$this->_set_vars[$key] = array('ob_start();'.$value.'$'.$key.'=ob_get_contents();ob_end_clean();');
preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches);
$this->_vars = array_merge($this->_vars, $matches[1]);
} elseif ($type == HTML_TEMPLATE_PLATON_SET_PARSE) {
$ar = $this->_stringTransform($value);
$this->_array = array_merge(
array('', 'ob_start();'), $ar,
array('$'.$key.'=ob_get_contents();ob_end_clean();'),
$this->_array);
$this->_prepended += count($ar) + 3;
} elseif ($type == HTML_TEMPLATE_PLATON_SET_VAR) {
$this->_set_vars[$key] = array('$'.$key.'=$'.$value.';');
} else { // HTML_TEMPLATE_PLATON_SET_VALUE
//echo "SET_VALUE: $key = $value
\n";
$this->_set_vars[$key] = $value;
}
}
return true;
} /* }}} */
function getParse() /* {{{ */
{
$ret = '';
foreach ($this->_set_vars as $key => $val) {
if (is_array($this->_set_vars[$key])) {
$ret .= $val[0];
} else {
$ret = '$'.$key.'=\''
.str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
.'\';'.$ret;
}
}
$ret = $this->_tag['begin_striped']
.$this->_tag['command_striped']
.str_replace("\n", '\'."\n".\'', $ret)
.$this->_tag['end_striped'] . "\n";
for ($i = 0; $i < count($this->_array); $i++) {
if ($i & 1) {
$ret .= $this->_tag['begin_striped']
.$this->_tag['command_striped']
.str_replace("\n", '\'."\n".\'', $this->_array[$i])
.$this->_tag['end_striped'];
} else {
$ret .= $this->_array[$i];
}
}
return $ret;
} /* }}} */
function getCode() /* {{{ */
{
$ret = '';
if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {
foreach ($this->_set_vars as $key => $val) {
// @ is used, since kept varaible may not exist
if (is_array($val)) {
$ret .= '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';'."\n";
$ret .= $val[0]."\n";
} else {
$ret = '$'.$key.'=\''
.str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
.'\';'."\n".$ret;
$ret = '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';'."\n".$ret;
}
}
} else {
foreach ($this->_set_vars as $key => $val) {
if (is_array($val)) {
$ret .= $val[0]."\n";
} else {
$ret = '$'.$key.'=\''
.str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
.'\';'."\n".$ret;
}
}
}
for ($i = 0; $i < count($this->_array); $i++) {
if ($i & 1) { // code
$ret .= $this->_array[$i];
} else { // normal string
$ret .= '?'.'>'.$this->_array[$i].'<'.'?php ';
}
$ret .= "\n";
}
if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {
foreach ($this->_set_vars as $key => $val) {
$ret .= '$'.$key.'=$'.$key.'__HTML_Template_Platon_var_bck;';
}
}
if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_FUNCTION) {
// Getting function name
global $_HTML_Template_Platon_cfg;
$function_name = $_HTML_Template_Platon_cfg['method_function_prefix']
.$_HTML_Template_Platon_cfg['method_function_idx'];
$_HTML_Template_Platon_cfg['method_function_idx']++;
return "if (! function_exists('$function_name')) {"
." function $function_name() {\n"
."$ret\n} }\n\n$function_name();";
}
return "{\n$ret\n}\n";
} /* }}} */
function getContent() /* {{{ */
{
ob_start();
eval($this->getCode());
$content = ob_get_contents();
ob_end_clean();
return $content;
} /* }}} */
function Output() /* {{{ */
{
return eval($this->getCode());
} /* }}} */
}
/* Modeline for ViM {{{
* vim: set ts=4:
* vim600: fdm=marker fdl=0 fdc=3:
* }}} */
?>