| verzia 1.8, 2002/08/01 17:36:07 |
verzia 1.9, 2002/08/01 20:52:58 |
|
|
| // 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. |
// 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; |
| Riadok 49 class phpTemplate |
|
| Riadok 49 class phpTemplate |
|
| 'file' => 'file' |
'file' => 'file' |
| ); |
); |
| |
|
| var $_filename = ''; |
|
| var $_array = array(); |
var $_array = array(); |
| var $_vars = array(); |
var $_vars = array(); |
| var $_set_vars = array(); |
var $_set_vars = null; |
| |
var $_method = null; |
| |
var $_filename = null; |
| var $_debug = 1; |
var $_debug = 1; |
| var $_prepended = 0; |
var $_prepended = 0; |
| var $_method = null; |
|
| |
|
| function phpTemplate($filename = '', $method = PHP_TEMPLATE_METHOD_NORMAL) |
function phpTemplate($filename = '', $method = PHP_TEMPLATE_METHOD_NORMAL) |
| { |
{ |
| Riadok 85 class phpTemplate |
|
| Riadok 85 class phpTemplate |
|
| $this->_method = $method; |
$this->_method = $method; |
| } |
} |
| |
|
| function load($filename) |
function _load($filename) |
| { |
{ |
| $this->_filename = $filename; |
if ($f = fopen($filename, 'r')) { |
| if (file_exists($filename)) { |
$data = ''; |
| $this->_array = $this->_stringTransform(join('', file($filename))); |
while ($tmp_data = fread($f, 1024)) { |
| |
$data .= $tmp_data; |
| return true; |
} |
| |
fclose($f); |
| |
return $this->_stringTransform($data); |
| } |
} |
| |
|
| if ($this->_debug) |
return array('phpTemplate DEBUG: file not found: '.$filename); |
| echo "phpTemplate DEBUG: file not found: $filename<br>"; |
} |
| |
|
| |
function load($filename) |
| |
{ |
| |
$this->_filename = $filename; |
| |
$this->_array = $this->_load($filename); |
| |
|
| return false; |
return true; |
| } |
} |
| |
|
| function reload() |
function reload() |
| { |
{ |
| $this->_set_vars = array(); |
$this->_set_vars = array(); |
| |
|
| if (empty($this->_filename)) { |
|
| return false; |
|
| } |
|
| |
|
| if (count($this->_array) > 0) { |
if (count($this->_array) > 0) { |
| if ($this->_prepended > 0) |
if ($this->_prepended > 0) |
| $this->_array = array_slice($this->_array, $this->_prepended); |
$this->_array = array_slice($this->_array, $this->_prepended); |
| Riadok 115 class phpTemplate |
|
| Riadok 118 class phpTemplate |
|
| return true; |
return true; |
| } |
} |
| |
|
| return $this->load($this->_filename); |
$this->_array = $this->_load($this->_filename); |
| |
return $this->_array !== false; |
| } |
} |
| |
|
| function getArray() |
function getArray() |
| Riadok 133 class phpTemplate |
|
| Riadok 137 class phpTemplate |
|
| { |
{ |
| return in_array($var, $this->_vars); |
return in_array($var, $this->_vars); |
| } |
} |
| |
|
| function _stringTransform($string) |
function _stringTransform($string) |
| { |
{ |
| // TODO: this is absolutely wrong!!! |
// TODO: this is absolutely wrong!!! |
| Riadok 147 class phpTemplate |
|
| Riadok 151 class phpTemplate |
|
| |
|
| preg_match_all('/\$(\w[\w\d_]*)/', $array[$i], $matches); |
preg_match_all('/\$(\w[\w\d_]*)/', $array[$i], $matches); |
| $this->_vars = array_merge($this->_vars, $matches[1]); |
$this->_vars = array_merge($this->_vars, $matches[1]); |
| //echo "adding: " . $matches[1] . "<br>"; |
|
| |
|
| if (! strncasecmp($array[$i], $this->_tag['command_striped'], |
if (! strncasecmp($array[$i], $this->_tag['command_striped'], |
| strlen($this->_tag['command_striped']))) { |
strlen($this->_tag['command_striped']))) { |
| $array[$i] = substr($array[$i], strlen($this->_tag['command_striped'])); |
$array[$i] = substr($array[$i], strlen($this->_tag['command_striped'])); |
| } |
} elseif (! strncasecmp($array[$i], $this->_tag['file_striped'], |
| elseif (! strncasecmp($array[$i], $this->_tag['file_striped'], |
|
| strlen($this->_tag['file_striped']))) { |
strlen($this->_tag['file_striped']))) { |
| $filename = dirname($this->_filename) . '/' |
$filename = dirname($this->_filename) . '/' |
| . trim(substr($array[$i], strlen($this->_tag['file_striped']))); |
. trim(substr($array[$i], strlen($this->_tag['file_striped']))); |
| if (file_exists($filename)) { |
$subarray = $this->_load($filename); |
| $subarray = $this->_stringTransform(join('', file($filename))); |
array_push($subarray, ''); |
| array_push($subarray, ''); |
array_unshift($subarray, ''); |
| array_unshift($subarray, ''); |
$array = array_merge(array_slice($array, 0, $i), $subarray, array_slice($array, $i + 1)); |
| |
$i += count($subarray) - 1; |
| $newarray = array(); |
|
| $newarray = array_merge( |
/* START & STOP support not finished yet! |
| array_slice($array, 0, $i), |
|
| $subarray, |
} elseif (! strncasecmp($array[$i], $this->_tag['stop'], |
| array_slice($array, $i + 1)); |
strlen($this->_tag['stop']))) { |
| $array = $newarray; |
$array[$i] = substr($array[$i], strlen($this->_tag['stop'])); |
| $i += count($subarray) - 1; |
} elseif (! strncasecmp($array[$i], $this->_tag['start'], |
| /* |
strlen($this->_tag['start']))) { |
| echo "<pre>"; |
$array[$i] = substr($array[$i], strlen($this->_tag['start'])); |
| echo "old i = $i<br>"; |
*/ |
| $i += count($subarray) - 1; |
|
| echo "new i = $i<br>"; |
|
| |
|
| //var_dump($newarray); |
|
| echo "</pre>"; |
|
| */ |
|
| } else { |
|
| $array[$i] = ''; |
|
| echo "Cannot find $filename!<br>"; |
|
| } |
|
| } elseif (! strncasecmp($array[$i], $this->_tag['stop'], |
|
| strlen($this->_tag['stop']))) { |
|
| $array[$i] = substr($array[$i], strlen($this->_tag['stop'])); |
|
| } elseif (! strncasecmp($array[$i], $this->_tag['start'], |
|
| 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] . ');'; |
| } |
} |
| Riadok 234 class phpTemplate |
|
| Riadok 221 class phpTemplate |
|
| } |
} |
| } |
} |
| } else { |
} else { |
| if (preg_match('/^[a-zA-Z_][\w\d_]*$/', $key)) { |
// Yeah, we will *not* check, if it is stupid template variable |
| if ($type == PHP_TEMPLATE_SET_CODE) { |
// if (preg_match('/^[a-zA-Z_][\w\d_]*$/', $key)) |
| $this->_set_vars[$key] = array($value); |
if ($type == PHP_TEMPLATE_SET_CODE) { |
| preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches); |
/* |
| $this->_vars = array_merge($this->_vars, $matches[1]); |
$this->_set_vars[$key] = array($value); |
| } elseif ($type == PHP_TEMPLATE_SET_PARSE) { |
preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches); |
| // TODO: lots of work |
$this->_vars = array_merge($this->_vars, $matches[1]); |
| $ar = $this->_stringTransform($value); |
*/ |
| |
|
| $this->_array = array_merge( |
$this->_set_vars[$key] = array('ob_start();'.$value.'$'.$key.'=ob_get_contents();ob_end_clean();'); |
| array('', 'ob_start();'), $ar, |
preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches); |
| array('$'.$key.' = ob_get_contents(); ob_end_clean();'), |
$this->_vars = array_merge($this->_vars, $matches[1]); |
| $this->_array); |
|
| $this->_prepended += 3; |
} elseif ($type == PHP_TEMPLATE_SET_PARSE) { |
| $this->_prepended += count($ar); |
// TODO: lots of work |
| } else { // PHP_TEMPLATE_SET_VALUE |
$ar = $this->_stringTransform($value); |
| $this->_set_vars[$key] = $value; |
|
| } |
$this->_array = array_merge( |
| |
array('', 'ob_start();'), $ar, |
| |
array('$'.$key.'=ob_get_contents();ob_end_clean();'), |
| |
$this->_array); |
| |
$this->_prepended += count($ar) + 3; |
| |
} else { // PHP_TEMPLATE_SET_VALUE |
| |
$this->_set_vars[$key] = $value; |
| } |
} |
| } |
} |
| |
|
| Riadok 260 class phpTemplate |
|
| Riadok 253 class phpTemplate |
|
| |
|
| function getParse() |
function getParse() |
| { |
{ |
| $ret .= $this->_tag['begin_striped'] . $this->_tag['command_striped']; |
$ret = $this->_tag['begin_striped'] . $this->_tag['command_striped']; |
| |
|
| foreach ($this->_set_vars as $key => $val) { |
foreach ($this->_set_vars as $key => $val) { |
| if (! is_array($val)) { |
if (is_array($this->_set_vars[$key])) { |
| $ret .= str_replace("\n", '\'."\n".\'', '$' . $key . '=\'' |
$ret .= $val[0]; |
| . str_replace('\'', '\\\'', |
} else { |
| str_replace('\\', '\\\\\\\\', $val)) . '\';'); |
$ret .= '$' . $key . '=\'' |
| |
. str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val)) . '\';'; |
| } |
} |
| } |
} |
| |
|
| Riadok 276 class phpTemplate |
|
| Riadok 270 class phpTemplate |
|
| if ($i & 1) { |
if ($i & 1) { |
| $ret .= $this->_tag['begin_striped'] |
$ret .= $this->_tag['begin_striped'] |
| . $this->_tag['command_striped'] |
. $this->_tag['command_striped'] |
| . str_replace("\n", '\'."\n".\'', |
. $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 326 class phpTemplate |
|
| Riadok 319 class phpTemplate |
|
| } |
} |
| |
|
| foreach ($this->_set_vars as $key => $val) { |
foreach ($this->_set_vars as $key => $val) { |
| if (! is_array($val)) { |
if (is_array($this->_set_vars[$key])) { |
| |
$ret .= $val[0]; |
| |
} else { |
| $ret .= ('$' . $key . '=\'' |
$ret .= ('$' . $key . '=\'' |
| . str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val)) |
. str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val)) |
| . '\';' . "\n"); |
. '\';' . "\n"); |
| Riadok 335 class phpTemplate |
|
| Riadok 330 class phpTemplate |
|
| |
|
| for ($i = 0; $i < count($this->_array); $i++) { |
for ($i = 0; $i < count($this->_array); $i++) { |
| if ($i & 1) { // code |
if ($i & 1) { // code |
| $ret .= $this->_simpleEchoTransform($this->_array[$i]); |
$ret .= $this->_array[$i]; |
| } else { // normal string |
} else { // normal string |
| |
|
| /* This old "echo" method was removed, |
/* This old "echo" method was removed, |
| Riadok 381 class phpTemplate |
|
| Riadok 376 class phpTemplate |
|
| return "{\n$ret\n}\n"; |
return "{\n$ret\n}\n"; |
| } |
} |
| |
|
| function _simpleEchoTransform($str) |
|
| { |
|
| //echo "<b>SIMPLE ECHO ... called with $str</b><br>"; |
|
| if (preg_match_all('/echo[(]\s*\$(\w[\w\d_]*)\s*[)];/', $str, $matches)) { |
|
| foreach ($matches[1] as $val) { |
|
| //echo "<b>than matched $val</b><br>"; |
|
| if (isset($this->_set_vars[$val]) && is_array($this->_set_vars[$val])) { |
|
| //echo "<b>and array founded containing [".$this->_set_vars[$val][0]."]</b><br>"; |
|
| $replacement = $this->_simpleEchoTransform($this->_set_vars[$val][0]); |
|
| $str = preg_replace('/echo[(]\s*\$'.$val.'\s*[)];/', $replacement, $str); |
|
| } |
|
| else { |
|
| //echo "<b>but no array founded</b><br>"; |
|
| } |
|
| } |
|
| } |
|
| |
|
| //echo "<b>so on exit we have $str</b><br>"; |
|
| |
|
| return $str; |
|
| } |
|
| |
|
| function getContent() |
function getContent() |
| { |
{ |
| ob_start(); |
ob_start(); |