Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Thursday, October 15, 2026

Diff for phpPlatonLib/HTML_Template_Platon/Platon.php between version 1.3 and 1.4

version 1.3, 2003/02/09 17:48:22 version 1.4, 2003/02/10 17:45:45
Line 1 
Line 1 
 <?php  <?php
   
 // Los humusos! Extremly fast!  if (! isset($GLOBALS)) { /* {{{ */
   
 /*  
    IMPORTANT TODO:  
    I *really* think, that storing already modified (I mean quoting issues)  
    variables in _set_vars array shoud increase performance noticeably!  
    NOT APPLICABLE NOW! It is printed "as is" outside <?php ?> tags.  
  */  
   
 if (! isset($GLOBALS)) {  
         global $GLOBALS;          global $GLOBALS;
 }  }
 if (! isset($GLOBALS['_HTML_Template_Platon_cfg'])) {  if (! isset($GLOBALS['_HTML_Template_Platon_cfg'])) {
Line 18  if (! isset($GLOBALS['_HTML_Template_Pla
Line 9  if (! isset($GLOBALS['_HTML_Template_Pla
                         'method_function_name' => '_HTML_Template_Platon_function_',                          'method_function_name' => '_HTML_Template_Platon_function_',
                         'method_function_idx'  => 0                          'method_function_idx'  => 0
                         );                          );
 }  } /* }}} */
   
 /* Output method (bit array) */  // Output method (bit array) {{{
 define('HTML_TEMPLATE_PLATON_METHOD_NORMAL',    0);  define('HTML_TEMPLATE_PLATON_METHOD_NORMAL',    0);
 define('HTML_TEMPLATE_PLATON_METHOD_FUNCTION',  1);  define('HTML_TEMPLATE_PLATON_METHOD_FUNCTION',  1);
 define('HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP',  2);  define('HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP',  2);
   // }}}
   
 /* Set method (enumeration constants) */  // Set method (enumeration constants) {{{
 define('HTML_TEMPLATE_PLATON_SET_VALUE',                0);  define('HTML_TEMPLATE_PLATON_SET_VALUE',                0);
 define('HTML_TEMPLATE_PLATON_SET_CODE',                 1);  define('HTML_TEMPLATE_PLATON_SET_CODE',                 1);
 define('HTML_TEMPLATE_PLATON_SET_PARSE',                2);  define('HTML_TEMPLATE_PLATON_SET_PARSE',                2);
 define('HTML_TEMPLATE_PLATON_SET_VAR',                  3);  define('HTML_TEMPLATE_PLATON_SET_VAR',                  3);
   // }}}
   
 class HTML_Template_Platon  class HTML_Template_Platon
 {  {
Line 58  class HTML_Template_Platon
Line 51  class HTML_Template_Platon
         var $_debug     = 1;          var $_debug     = 1;
         var $_prepended = 0;          var $_prepended = 0;
   
         function HTML_Template_Platon($filename = '', $method = HTML_TEMPLATE_PLATON_METHOD_NORMAL)          function HTML_Template_Platon($filename = '', /* ...) {{{ */
                           $method = HTML_TEMPLATE_PLATON_METHOD_NORMAL)
         {          {
                 global $_HTML_Template_Platon_cfg;                  global $_HTML_Template_Platon_cfg;
   
                 foreach (array_keys($this->_tag) as $key) {                  foreach (array_keys($this->_tag) as $key) {
                         $this->_tag[$key]            = preg_quote($this->_tag[$key]);                          $this->_tag[$key]            = preg_quote($this->_tag[$key]);
                         $this->_tag[$key.'_striped'] = stripslashes($this->_tag[$key]);                          $this->_tag[$key.'_striped'] = stripslashes($this->_tag[$key]);
                 }                  }
   
                 $this->_method   = $method;                  $this->_method   = $method;
                 $this->_filename = $filename;                  $this->_filename = $filename;
                 $this->_load_fnc = $_HTML_Template_Platon_cfg['load_function'];                  $this->_load_fnc = $_HTML_Template_Platon_cfg['load_function'];
                 $this->reload();                  $this->reload();
   
                 return true;                  return true;
         }          } /* }}} */
   
         function setTags($begin_tag = '', $end_tag = '')          function setTags($begin_tag = '', $end_tag = '') /* {{{ */
         {          {
                 if (strlen($begin_tag) > 0)                  if (strlen($begin_tag) > 0) {
                         $this->_tag['begin'] = $begin_tag;                          $this->_tag['begin'] = $begin_tag;
                   }
                 if (strlen($end_tag) > 0)                  if (strlen($end_tag) > 0) {
                         $this->_tag['end'] = $end_tag;                          $this->_tag['end'] = $end_tag;
         }                  }
           } /* }}} */
   
         function setMethod($method)          function setMethod($method) /* {{{ */
         {          {
                 $this->_method = $method;                  $this->_method = $method;
         }          } /* }}} */
   
         function setLoadFunction($function_name)          function setLoadFunction($function_name) /* {{{ */
         {          {
                 if (isset($this)) {                  if (isset($this)) {
                         $this->_load_fnc = $function_name;                          $this->_load_fnc = $function_name;
Line 97  class HTML_Template_Platon
Line 89  class HTML_Template_Platon
                         global $_HTML_Template_Platon_cfg;                          global $_HTML_Template_Platon_cfg;
                         $_HTML_Template_Platon_cfg['load_function'] = $function_name;                          $_HTML_Template_Platon_cfg['load_function'] = $function_name;
                 }                  }
         }          } /* }}} */
   
         function _load($filename)          function _load($filename) /* {{{ */
         {          {
                 if (isset($this->_load_fnc)) {                  if (isset($this->_load_fnc)) {
                         $load_fnc = $this->_load_fnc;                          $load_fnc = $this->_load_fnc;
Line 113  class HTML_Template_Platon
Line 105  class HTML_Template_Platon
                                 fclose($f);                                  fclose($f);
                         }                          }
                 }                  }
   
                 if (! isset($data) || $data === false) {                  if (! isset($data) || $data === false) {
                         return array('<b>HTML_Template_Platon ERROR</b>: cannot load template with key '."'$filename'");                          return array('<b>HTML_Template_Platon ERROR</b>: cannot load template with key '."'$filename'");
                 }                  }
   
                 return $this->_stringTransform($data);                  return $this->_stringTransform($data);
         }          } /* }}} */
   
         function load($filename)          function load($filename) /* {{{ */
         {          {
                 $this->_filename = $filename;                  $this->_filename = $filename;
                 $this->_array    = $this->_load($filename);                  $this->_array    = $this->_load($filename);
   
                 return true;                  return true;
         }          } /* }}} */
   
         function reload()          function reload() /* {{{ */
         {          {
                 $this->_set_vars = array();                  $this->_set_vars = array();
   
                 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);
                         $this->_prepended = 0;                          $this->_prepended = 0;
                         return true;                          return true;
                 }                  }
   
                 $this->_array = $this->_load($this->_filename);                  $this->_array = $this->_load($this->_filename);
                 return $this->_array !== false;                  return $this->_array !== false;
         }          } /* }}} */
   
         function getArray()          function getArray() /* {{{ */
         {          {
                 return $this->_array;                  return $this->_array;
         }          } /* }}} */
   
         function getVars()          function getVars() /* {{{ */
         {          {
                 $this->_vars = array_unique($this->_vars);                  $this->_vars = array_unique($this->_vars);
                 return $this->_vars;                  return $this->_vars;
         }          } /* }}} */
   
         function isVar($var)          function isVar($var) /* {{{ */
         {          {
                 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 regexp needs to be improved
                 $string = str_replace($this->_tag['end_striped'], $this->_tag['end_striped'] . "\n", $string);                  $string = str_replace($this->_tag['end_striped'], $this->_tag['end_striped'] . "\n", $string);
                 $regexp = $this->_tag['begin'] . '(.+)' . $this->_tag['end'] . '[\n]';                  $regexp = $this->_tag['begin'] . '(.+)' . $this->_tag['end'] . '[\n]';
                 $array  = preg_split("/$regexp/im", $string, -1, PREG_SPLIT_DELIM_CAPTURE);                  $array  = preg_split("/$regexp/im", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
   
                 for ($i = 1; $i < count($array); $i += 2) {                  for ($i = 1; $i < count($array); $i += 2) {
   
                         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]);
   
Line 184  class HTML_Template_Platon
Line 170  class HTML_Template_Platon
                                 array_unshift($subarray, '');                                  array_unshift($subarray, '');
                                 $array = array_merge(array_slice($array, 0, $i), $subarray, array_slice($array, $i + 1));                                  $array = array_merge(array_slice($array, 0, $i), $subarray, array_slice($array, $i + 1));
                                 $i += count($subarray) - 1;                                  $i += count($subarray) - 1;
   
                                 /* START & STOP support not finished yet!  
   
                                    } 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] . ');';
                         }                          }
                 }                  }
   
                 if (0) {  
                         echo '<h3>RETURNING FROM _stringTransform()</h3><pre>';  
                         ob_start();  
                         var_dump($array);  
                         $contents = ob_get_contents(); ob_end_clean();  
                         $contents = htmlspecialchars($contents);  
                         echo preg_replace('/(\[\d+\]=\&gt;)/', '<b>\1</b>', $contents);  
                         echo '</pre>';  
                 }  
   
                 return $array;                  return $array;
         }          } /* }}} */
   
         // TODO: constants instead of numbers 1, 2, ...  
   
         function setParse($key, $value = '')          function setParse($key, $value = '') /* {{{ */
         {          {
                 $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_PARSE);                  $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_PARSE);
         }          } /* }}} */
   
         function setCode($key, $value = '')          function setCode($key, $value = '') /* {{{ */
         {          {
                 $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_CODE);                  $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_CODE);
         }          } /* }}} */
   
         function setVar($key, $value = '')          function setVar($key, $value = '') /* {{{ */
         {          {
                 $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_VAR);                  $this->set($key, $value, HTML_TEMPLATE_PLATON_SET_VAR);
         }          } /* }}} */
   
         function setValue($key, $value = '')          function setValue($key, $value = '') /* {{{ */
         {          {
                 $this->set($key, $value);                  $this->set($key, $value);
         }          } /* }}} */
   
         function set($key, $value = '', $type = HTML_TEMPLATE_PLATON_SET_VALUE)          function set($key, $value = '', /* ...) {{{ */
                           $type = HTML_TEMPLATE_PLATON_SET_VALUE)
         {          {
                 if (is_array($key)) {                  if (is_array($key)) {
                         while (list($k, $v) = each($key)) {                          while (list($k, $v) = each($key)) {
                                 if (! empty($k)) {                                  if (! empty($k)) {
                                         /* Empty values are allowed. Empty keys are NOT. */                                          // Empty values are allowed. Empty keys are NOT.
   
                                         $this->set($k, $v, $type);                                          $this->set($k, $v, $type);
                                 }                                  }
                         }                          }
                 } else {                  } else {
                         if ($type == HTML_TEMPLATE_PLATON_SET_CODE) {                          if ($type == HTML_TEMPLATE_PLATON_SET_CODE) {
                                 /*  
                                    $this->_set_vars[$key] = array($value);  
                                    preg_match_all('/\$(\w[\w\d_]*)/', $value, $matches);  
                                    $this->_vars = array_merge($this->_vars, $matches[1]);  
                                  */  
   
                                 $this->_set_vars[$key] = array('ob_start();'.$value.'$'.$key.'=ob_get_contents();ob_end_clean();');                                  $this->_set_vars[$key] = array('ob_start();'.$value.'$'.$key.'=ob_get_contents();ob_end_clean();');
                                 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 == HTML_TEMPLATE_PLATON_SET_PARSE) {                          } elseif ($type == HTML_TEMPLATE_PLATON_SET_PARSE) {
                                 // TODO: lots of work  
                                 $ar = $this->_stringTransform($value);                                  $ar = $this->_stringTransform($value);
                                 if (0) {  
                                         echo '<hr><hr><hr><hr>';  
                                         echo '<hr><pre><font color=white>SETTING PARSE<br>KEY = '.$key.'<br>VALUE = '.htmlentities($value).'<br>';  
                                         echo 'AFTER _sT = ';  
                                         ob_start();  
                                         var_dump($ar);  
                                         $xx = ob_get_contents();  
                                         ob_end_clean();  
                                         echo htmlentities($xx);  
                                         echo '</font></pre><hr>';  
                                         //exit;  
                                 }  
                                 $this->_array = array_merge(                                  $this->_array = array_merge(
                                                 array('', 'ob_start();'), $ar,                                                  array('', 'ob_start();'), $ar,
                                                 array('$'.$key.'=ob_get_contents();ob_end_clean();'),                                                  array('$'.$key.'=ob_get_contents();ob_end_clean();'),
Line 282  class HTML_Template_Platon
Line 225  class HTML_Template_Platon
                                 $this->_set_vars[$key] = $value;                                  $this->_set_vars[$key] = $value;
                         }                          }
                 }                  }
   
                 return true;                  return true;
         }          } /* }}} */
   
         function getParse()          function getParse() /* {{{ */
         {          {
                 $ret = '';                  $ret = '';
                 foreach ($this->_set_vars as $key => $val) {                  foreach ($this->_set_vars as $key => $val) {
                         if (is_array($this->_set_vars[$key])) {                          if (is_array($this->_set_vars[$key])) {
                                 $ret .= $val[0];                                  $ret .= $val[0];
                         } else {                          } else {
                                 $ret = '$' . $key . '=\''                                  $ret = '$'.$key.'=\''
                                         . str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val)) . '\';'                                          .str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
                                         . $ret;                                          .'\';'.$ret;
                         }                          }
                 }                  }
   
                 $ret = $this->_tag['begin_striped']                  $ret = $this->_tag['begin_striped']
                         . $this->_tag['command_striped']                          .$this->_tag['command_striped']
                         . str_replace("\n", '\'."\n".\'', $ret)                          .str_replace("\n", '\'."\n".\'', $ret)
                         . $this->_tag['end_striped'] . "\n";                          .$this->_tag['end_striped'] . "\n";
   
                 for ($i = 0; $i < count($this->_array); $i++) {                  for ($i = 0; $i < count($this->_array); $i++) {
                         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])                                          .str_replace("\n", '\'."\n".\'', $this->_array[$i])
                                         . $this->_tag['end_striped'];                                          .$this->_tag['end_striped'];
                         } else {                          } else {
                                 $ret .= $this->_array[$i];                                  $ret .= $this->_array[$i];
                         }                          }
                 }                  }
                 /*  
                    echo "<pre>getParse():\n";  
                    echo htmlentities($ret);  
                    echo "</pre>";  
                    exit;  
                  */  
                 return $ret;                  return $ret;
         }          } /* }}} */
   
         function getCode()          function getCode() /* {{{ */
         {          {
                 $ret = '';                  $ret = '';
                 /*  
                    foreach ($this->_vars as $key) {  
                    if (isset($this->_set_vars[$key])) {  
                    $val = $this->_set_vars[$key];  
                    if (! is_array($val)) {  
                    $ret .= ('$' . $key . '=\''  
                    . str_replace("'", "\'",  
                    str_replace("\\", "\\\\", $val)) . '\';'  
                    . "\n");  
                    }  
                    }  
                    else {  
                    $ret .= ('if (!isset($'.$key.'))$' . $key . '=\'\';' . "\n");  
                    }  
                    }  
                  */  
   
                 /* Note:  
                    This was old subtitution code. But I turned on full error reporting  
                    in phpMyEzin project and phptemplate causes undefined variable error.  
                  */  
   
                 if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {                  if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {
                         foreach ($this->_set_vars as $key => $val) {                          foreach ($this->_set_vars as $key => $val) {
                                 // @ is used, since kept varaible may not exist                                  // @ is used, since kept varaible may not exist
Line 355  class HTML_Template_Platon
Line 267  class HTML_Template_Platon
                                         $ret .= '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';'."\n";                                          $ret .= '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';'."\n";
                                         $ret .= $val[0]."\n";                                          $ret .= $val[0]."\n";
                                 } else {                                  } else {
                                         $ret = '$' . $key . '=\''                                          $ret = '$'.$key.'=\''
                                                 . str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))                                                  .str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
                                                 . '\';' . "\n"                                                  .'\';'."\n".$ret;
                                                 . $ret;                                          $ret = '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';'."\n".$ret;
                                         $ret = '$'.$key.'__HTML_Template_Platon_var_bck=@$'.$key.';' . "\n" . $ret;  
                                 }                                  }
                         }                          }
                 } else {                  } else {
Line 367  class HTML_Template_Platon
Line 278  class HTML_Template_Platon
                                 if (is_array($val)) {                                  if (is_array($val)) {
                                         $ret .= $val[0]."\n";                                          $ret .= $val[0]."\n";
                                 } else {                                  } else {
                                         $ret = '$' . $key . '=\''                                          $ret = '$'.$key.'=\''
                                                 . str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))                                                  .str_replace('\'', '\\\'', str_replace('\\', '\\\\\\\\', $val))
                                                 . '\';' . "\n"                                                  .'\';'."\n".$ret;
                                                 . $ret;  
                                 }                                  }
                         }                          }
                 }                  }
   
                 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->_array[$i];                                  $ret .= $this->_array[$i];
                         } else { // normal string                          } else { // normal string
   
                                 /* This old "echo" method was removed,  
                                    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 .= '?>'.$this->_array[$i].'<?php ';
                         }                          }
                         $ret .= "\n";                          $ret .= "\n";
                 }                  }
   
                 if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {                  if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_VAR_KEEP) {
                         foreach ($this->_set_vars as $key => $val) {                          foreach ($this->_set_vars as $key => $val) {
                                 $ret .= '$'.$key.'=$'.$key.'__HTML_Template_Platon_var_bck;';                                  $ret .= '$'.$key.'=$'.$key.'__HTML_Template_Platon_var_bck;';
                         }                          }
                 }                  }
   
                 if (0) {  
                         echo '<h3>CODE</h3><pre>' . "\n";  
                         echo htmlspecialchars($ret);  
                         echo '</pre>';  
                 }  
   
                 if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_FUNCTION) {                  if ($this->_method & HTML_TEMPLATE_PLATON_METHOD_FUNCTION) {
                         /* getting function name */                          // Getting function name
                         global $_HTML_Template_Platon_cfg;                          global $_HTML_Template_Platon_cfg;
                         $function_name = $_HTML_Template_Platon_cfg['method_function_prefix']                          $function_name = $_HTML_Template_Platon_cfg['method_function_prefix']
                                 . $_HTML_Template_Platon_cfg['method_function_idx'];                                  .$_HTML_Template_Platon_cfg['method_function_idx'];
                         $_HTML_Template_Platon_cfg['method_function_idx']++;                          $_HTML_Template_Platon_cfg['method_function_idx']++;
   
                         return "if (! function_exists('$function_name')) {"                          return "if (! function_exists('$function_name')) {"
                                 ." function $function_name() {\n"                                  ." function $function_name() {\n"
                                 . "$ret\n} }\n\n$function_name();";                                  ."$ret\n} }\n\n$function_name();";
                 }                  }
   
                 return "{\n$ret\n}\n";                  return "{\n$ret\n}\n";
         }          } /* }}} */
   
         function getContent()          function getContent() /* {{{ */
         {          {
                 ob_start();                  ob_start();
                 //echo "<hr><pre>" . htmlentities($this->getCode()) . "</pre>";  
                 eval($this->getCode());                  eval($this->getCode());
                 $content = ob_get_contents();                  $content = ob_get_contents();
                 ob_end_clean();                  ob_end_clean();
                 return $content;                  return $content;
         }          } /* }}} */
   
         function Output()          function Output() /* {{{ */
         {          {
                 //echo "<hr><pre>" . htmlentities($this->getCode()) . "</pre>";  
                 return eval($this->getCode());                  return eval($this->getCode());
         }          } /* }}} */
   
 }  }
   
   /* Modeline for ViM {{{
    * vim: set ts=4:
    * vim600: fdm=marker fdl=0 fdc=3:
    * }}} */
   
 ?>  ?>

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top