| verzia 1.6, 2002/07/23 19:41:20 |
verzia 1.7, 2002/07/24 15:24:52 |
|
|
| |
|
| // Los humusos! Extremly fast! |
// Los humusos! Extremly fast! |
| |
|
| |
/* |
| // IMPORTANT TODO: |
// IMPORTANT TODO: |
| // I *really* think, that storing already modified (I mean quoting issues) |
// I *really* think, that storing already modified (I mean quoting issues) |
| // variables in _set_vars array shoud increase performance noticeably! |
// variables in _set_vars array shoud increase performance noticeably! |
| |
// NOT APPLICABLE NOW! It is printed "as is" outside <?php ?> tags. |
| |
*/ |
| |
|
| if (! isset($__phpTemplate_fnc_idx)) { |
if (! isset($__phpTemplate_fnc_idx)) { |
| $__phpTemplate_fnc_idx = 0; |
$__phpTemplate_fnc_idx = 0; |
| } |
} |
| |
|
| |
/* Output method (bit array) */ |
| define(PHP_TEMPLATE_METHOD_NORMAL, 0); |
define(PHP_TEMPLATE_METHOD_NORMAL, 0); |
| define(PHP_TEMPLATE_METHOD_FUNCTION, 1); |
define(PHP_TEMPLATE_METHOD_FUNCTION, 1); |
| define(PHP_TEMPLATE_METHOD_VAR_KEEP, 2); |
define(PHP_TEMPLATE_METHOD_VAR_KEEP, 2); |
| |
|
| |
/* Set method (enumeration constants) */ |
| |
define(PHP_TEMPLATE_SET_VALUE, 0); |
| |
define(PHP_TEMPLATE_SET_CODE, 1); |
| |
define(PHP_TEMPLATE_SET_PARSE, 2); |
| |
|
| class phpTemplate |
class phpTemplate |
| { |
{ |
| var $_cstr = array( |
var $_cstr = array( |
| 'begin' => '<!--', |
'begin' => '<!--', |
| 'end' => '-->', |
'end' => '-->', |
| 'classname' => 'phpTemplate.class.php', |
'classname' => 'phpTemplate.class.php', |
| 'version' => '0.1' |
'version' => '0.2' |
| ); |
); |
| |
|
| var $_tag = array( |
var $_tag = array( |
| 'begin' => '<|', |
'begin' => '<|', |
| //'delim' => ':', |
//'delim' => ':', |
| 'command' => 'cmd|', |
'command' => 'cmd|', |
| 'file' => 'include|', |
'file' => 'include|', |
| 'start' => 'start', |
'start' => 'start', |
| 'stop' => 'stop', |
'stop' => 'stop', |
| 'end' => '|>' |
'end' => '|>' |
| ); |
); |
| |
|
| var $_cmd = array( /* Both sides must be lowercase. */ |
var $_cmd = array( /* Both sides must be lowercase. */ |
| 'if' => 'if', |
'if' => 'if', |
| 'else' => 'else', |
'else' => 'else', |
| 'endif' => 'endif', |
'endif' => 'endif', |
| 'file' => 'file' |
'file' => 'file' |
| ); |
); |
| |
|
| var $_filename = ''; |
var $_filename = ''; |
| var $_array = array(); |
var $_array = array(); |
| var $_vars = array(); |
var $_vars = array(); |
| var $_set_vars = array(); |
var $_set_vars = array(); |
| var $_debug = 1; |
var $_debug = 1; |
| var $_method; |
var $_prepended = 0; |
| |
var $_method = null; |
| |
|
| function phpTemplate($filename = '', $method = PHP_TEMPLATE_METHOD_NORMAL) |
function phpTemplate($filename = '', $method = PHP_TEMPLATE_METHOD_NORMAL) |
| { |
{ |
| Riadok 70 class phpTemplate |
|
| Riadok 80 class phpTemplate |
|
| $this->_tag['end'] = $end_tag; |
$this->_tag['end'] = $end_tag; |
| } |
} |
| |
|
| |
function setMethod($method) |
| |
{ |
| |
$this->_method = $method; |
| |
} |
| |
|
| function load($filename) |
function load($filename) |
| { |
{ |
| $this->_filename = $filename; |
$this->_filename = $filename; |
| //echo "Loading file $filename: "; |
if (file_exists($filename)) { |
| if (file_exists($filename)) { # DEBUG |
|
| $this->_array = $this->_stringTransform(join('', file($filename))); |
$this->_array = $this->_stringTransform(join('', file($filename))); |
| //echo "OK<br>"; |
|
| |
|
| return true; |
return true; |
| } |
} |
| Riadok 84 class phpTemplate |
|
| Riadok 97 class phpTemplate |
|
| if ($this->_debug) |
if ($this->_debug) |
| echo "phpTemplate DEBUG: file not found: $filename<br>"; |
echo "phpTemplate DEBUG: file not found: $filename<br>"; |
| |
|
| //echo "failed<br>"; |
|
| return false; |
return false; |
| } |
} |
| |
|
| function reload() |
function reload() |
| { |
{ |
| if (! empty($this->_filename)) |
$this->_set_vars = array(); |
| return $this->load($this->_filename); |
|
| |
|
| return false; |
if (empty($this->_filename)) { |
| |
return false; |
| |
} |
| |
|
| |
if (count($this->_array) > 0) { |
| |
if ($this->_prepended > 0) |
| |
$this->_array = array_slice($this->_array, $this->_prepended); |
| |
$this->_prepended = 0; |
| |
return true; |
| |
} |
| |
|
| |
return $this->load($this->_filename); |
| } |
} |
| |
|
| function getArray() |
function getArray() |
| Riadok 109 class phpTemplate |
|
| Riadok 131 class phpTemplate |
|
| |
|
| function isVar($var) |
function isVar($var) |
| { |
{ |
| return (array_search($var, $this->_vars) == false ? false : true); |
return in_array($var, $this->_vars); |
| } |
} |
| |
|
| function _stringTransform($string) |
function _stringTransform($string) |
| Riadok 156 class phpTemplate |
|
| Riadok 178 class phpTemplate |
|
| //var_dump($newarray); |
//var_dump($newarray); |
| echo "</pre>"; |
echo "</pre>"; |
| */ |
*/ |
| } |
} else { |
| else { |
|
| $array[$i] = ''; |
$array[$i] = ''; |
| echo "Cannot find $filename!<br>"; |
echo "Cannot find $filename!<br>"; |
| } |
} |
| } |
} elseif (! strncasecmp($array[$i], $this->_tag['stop'], |
| elseif (! strncasecmp($array[$i], $this->_tag['stop'], |
|
| strlen($this->_tag['stop']))) { |
strlen($this->_tag['stop']))) { |
| $array[$i] = substr($array[$i], strlen($this->_tag['stop'])); |
$array[$i] = substr($array[$i], strlen($this->_tag['stop'])); |
| } |
} elseif (! strncasecmp($array[$i], $this->_tag['start'], |
| elseif (! strncasecmp($array[$i], $this->_tag['start'], |
|
| strlen($this->_tag['start']))) { |
strlen($this->_tag['start']))) { |
| $array[$i] = substr($array[$i], strlen($this->_tag['start'])); |
$array[$i] = substr($array[$i], strlen($this->_tag['start'])); |
| } |
} else { |
| else { |
|
| $array[$i] = 'echo(' . $array[$i] . ');'; |
$array[$i] = 'echo(' . $array[$i] . ');'; |
| } |
} |
| |
|
| } |
} |
| |
|
| //echo "<pre>RETURNING FROM _stringTransform()"; |
if (0) { |
| //var_dump($array); |
echo '<h3>RETURNING FROM _stringTransform()</h3><pre>'; |
| //echo "</pre>"; |
ob_start(); |
| |
var_dump($array); |
| |
$contents = ob_get_contents(); ob_end_clean(); |
| |
$contents = htmlspecialchars($contents); |
| |
echo preg_replace('/(\[\d+\]=\>)/', '<b>\1</b>', $contents); |
| |
echo '</pre>'; |
| |
} |
| |
|
| return $array; |
return $array; |
| } |
} |
| Riadok 187 class phpTemplate |
|
| Riadok 210 class phpTemplate |
|
| |
|
| function setParse($key, $value = '') |
function setParse($key, $value = '') |
| { |
{ |
| $this->set($key, $value, 2); |
$this->set($key, $value, PHP_TEMPLATE_SET_PARSE); |
| } |
} |
| |
|
| function setCode($key, $value = '') |
function setCode($key, $value = '') |
| { |
{ |
| $this->set($key, $value, 1); |
$this->set($key, $value, PHP_TEMPLATE_SET_CODE); |
| } |
} |
| |
|
| function setValue($key, $value = '') |
function setValue($key, $value = '') |
| Riadok 200 class phpTemplate |
|
| Riadok 223 class phpTemplate |
|
| $this->set($key, $value); |
$this->set($key, $value); |
| } |
} |
| |
|
| function set($key, $value = '', $type = 0) |
function set($key, $value = '', $type = PHP_TEMPLATE_SET_VALUE) |
| { |
{ |
| if (is_array($key)) { |
if (is_array($key)) { |
| while (list($k, $v) = each($key)) { |
while (list($k, $v) = each($key)) { |
| Riadok 212 class phpTemplate |
|
| Riadok 235 class phpTemplate |
|
| } |
} |
| } else { |
} else { |
| if (preg_match('/^[a-zA-Z_][\w\d_]*$/', $key)) { |
if (preg_match('/^[a-zA-Z_][\w\d_]*$/', $key)) { |
| if ($type == 1) { |
if ($type == PHP_TEMPLATE_SET_CODE) { |
| $this->_set_vars[$key] = array($value); |
$this->_set_vars[$key] = array($value); |
| preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches); |
preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches); |
| $this->_vars = array_merge($this->_vars, $matches[1]); |
$this->_vars = array_merge($this->_vars, $matches[1]); |
| } elseif ($type == 2) { |
} elseif ($type == PHP_TEMPLATE_SET_PARSE) { |
| // TODO: lots of work |
// TODO: lots of work |
| $ar = $this->_stringTransform($value); |
$ar = $this->_stringTransform($value); |
| |
|
| Riadok 224 class phpTemplate |
|
| Riadok 247 class phpTemplate |
|
| array('', 'ob_start();'), $ar, |
array('', 'ob_start();'), $ar, |
| array('$'.$key.' = ob_get_contents(); ob_end_clean();'), |
array('$'.$key.' = ob_get_contents(); ob_end_clean();'), |
| $this->_array); |
$this->_array); |
| } else { |
$this->_prepended += 3; |
| |
$this->_prepended += count($ar); |
| |
} else { // PHP_TEMPLATE_SET_VALUE |
| $this->_set_vars[$key] = $value; |
$this->_set_vars[$key] = $value; |
| } |
} |
| } |
} |
| Riadok 248 class phpTemplate |
|
| Riadok 273 class phpTemplate |
|
| $ret .= $this->_tag['end_striped']; |
$ret .= $this->_tag['end_striped']; |
| |
|
| for ($i = 0; $i < count($this->_array); $i++) { |
for ($i = 0; $i < count($this->_array); $i++) { |
| // TODO: byte counting (last bit check) |
if ($i & 1) { |
| if ($i % 2) { |
|
| $ret .= $this->_tag['begin_striped'] |
$ret .= $this->_tag['begin_striped'] |
| . $this->_tag['command_striped'] |
. $this->_tag['command_striped'] |
| . str_replace("\n", '\'."\n".\'', |
. str_replace("\n", '\'."\n".\'', |
| $this->_simpleEchoTransform($this->_array[$i])) |
$this->_simpleEchoTransform($this->_array[$i])) |
| . $this->_tag['end_striped']; |
. $this->_tag['end_striped']; |
| } |
} else { |
| else { |
|
| $ret .= $this->_array[$i]; |
$ret .= $this->_array[$i]; |
| } |
} |
| } |
} |
| Riadok 311 class phpTemplate |
|
| Riadok 334 class phpTemplate |
|
| } |
} |
| |
|
| for ($i = 0; $i < count($this->_array); $i++) { |
for ($i = 0; $i < count($this->_array); $i++) { |
| // TODO: byte counting (last bit check) |
if ($i & 1) { // code |
| if ($i % 2) { |
|
| $ret .= $this->_simpleEchoTransform($this->_array[$i]); |
$ret .= $this->_simpleEchoTransform($this->_array[$i]); |
| } else { |
} else { // normal string |
| $ret .= ('echo(\'' . |
|
| str_replace("'", "\'", |
/* This old "echo" method was removed, |
| str_replace("\\", "\\\\", $this->_array[$i])) |
but it is kept for testing and so on. |
| . '\');'); |
|
| |
$ret .= ('echo(\'' . |
| |
str_replace("'", "\'", |
| |
str_replace("\\", "\\\\", $this->_array[$i])) |
| |
. '\');'); |
| |
*/ |
| |
|
| |
/* This one is a little bit (0.3s on few templates) faster */ |
| |
$ret .= '?>'.$this->_array[$i].'<?php '; |
| } |
} |
| $ret .= "\n"; |
$ret .= "\n"; |
| } |
} |
| Riadok 331 class phpTemplate |
|
| Riadok 361 class phpTemplate |
|
| } |
} |
| } |
} |
| |
|
| /* |
if (0) { |
| echo "<pre>CODE:\n"; |
echo '<h3>CODE</h3><pre>' . "\n"; |
| echo htmlentities($ret); |
echo htmlspecialchars($ret); |
| echo "</pre>"; |
echo '</pre>'; |
| */ |
} |
| |
|
| if ($this->_method & PHP_TEMPLATE_METHOD_FUNCTION) { |
if ($this->_method & PHP_TEMPLATE_METHOD_FUNCTION) { |
| /* getting function name */ |
/* getting function name */ |