diff --git a/src/Pluf/Template.php b/src/Pluf/Template.php index 889f674..f5bd888 100644 --- a/src/Pluf/Template.php +++ b/src/Pluf/Template.php @@ -32,7 +32,7 @@ class Pluf_Template public $compiled_template = ''; public $template_content = ''; public $context = null; - + public $class = ''; /** * Constructor. * @@ -48,12 +48,12 @@ class Pluf_Template function __construct($template, $folders=null, $cache=null) { $this->tpl = $template; - if (is_null($folders)) { + if (null == $folders) { $this->folders = Pluf::f('template_folders'); } else { $this->folders = $folders; } - if (is_null($cache)) { + if (null == $cache) { $this->cache = Pluf::f('tmp_folder'); } else { $this->cache = $cache; @@ -63,6 +63,18 @@ class Pluf_Template $GLOBALS['_PX_tests_templates'] = array(); } } + $this->compiled_template = $this->getCompiledTemplateName(); + $b = $this->compiled_template[1]; + $this->class = 'Pluf_Template_'.$b; + $this->compiled_template = $this->compiled_template[0]; + if (!class_exists($this->class, false)) { + if (!file_exists($this->compiled_template) or Pluf::f('debug')) { + $compiler = new Pluf_Template_Compiler($this->tpl, $this->folders); + $this->template_content = $compiler->getCompiledTemplate(); + $this->write($b); + } + include $this->compiled_template; + } } /** @@ -75,20 +87,15 @@ class Pluf_Template if (defined('IN_UNIT_TESTS')) { $GLOBALS['_PX_tests_templates'][] = $this; } - $this->compiled_template = $this->getCompiledTemplateName(); - if (!file_exists($this->compiled_template) or Pluf::f('debug')) { - $compiler = new Pluf_Template_Compiler($this->tpl, $this->folders); - $this->template_content = $compiler->getCompiledTemplate(); - $this->write(); - } - if (is_null($c)) { + if (null == $c) { $c = new Pluf_Template_Context(); } $this->context = $c; ob_start(); $t = $c; try { - include $this->compiled_template; + call_user_func(array($this->class, 'render'), $t); + //include $this->compiled_template; } catch (Exception $e) { ob_clean(); throw $e; @@ -112,7 +119,8 @@ class Pluf_Template // The compiled template not only depends on the file but also // on the possible folders in which it can be found. $_tmp = var_export($this->folders, true); - return $this->cache.'/Pluf_Template-'.md5($_tmp.$this->tpl).'.phps'; + return array($this->cache.'/Pluf_Template-'.md5($_tmp.$this->tpl).'.phps', + md5($_tmp.$this->tpl)); } /** @@ -121,8 +129,10 @@ class Pluf_Template * * @return bool Success in writing */ - function write() + function write($name) { + $this->template_content = ''.$this->template_content.'compiled_template, 'a'); if ($fp !== false) { @@ -217,18 +227,14 @@ function Pluf_Template_timeFormat($time, $format='Y-m-d H:i:s') */ function Pluf_Template_safeEcho($mixed, $echo=true) { - if (!is_object($mixed) or 'Pluf_Template_SafeString' !== get_class($mixed)) { - if ($echo) { - echo htmlspecialchars((string) $mixed, ENT_COMPAT, 'UTF-8'); - } else { - return htmlspecialchars((string) $mixed, ENT_COMPAT, 'UTF-8'); - } + if ($echo) { + echo ('Pluf_Template_SafeString' !== get_class($mixed)) ? + htmlspecialchars($mixed, ENT_COMPAT, 'UTF-8') : + $mixed->value; } else { - if ($echo) { - echo $mixed->value; - } else { - return $mixed->value; - } + return ('Pluf_Template_SafeString' !== get_class($mixed)) ? + htmlspecialchars($mixed, ENT_COMPAT, 'UTF-8') : + $mixed->value; } } diff --git a/src/Pluf/Template/Compiler.php b/src/Pluf/Template/Compiler.php index 31c7dfd..9ad2d81 100644 --- a/src/Pluf/Template/Compiler.php +++ b/src/Pluf/Template/Compiler.php @@ -622,7 +622,8 @@ class Pluf_Template_Compiler if ($type == T_STRING && $inDot) { $result .= $str; } elseif ($type == T_VARIABLE) { - $result .= '$t->_vars[\''.substr($str, 1).'\']'; + //$result .= '$t->_vars[\''.substr($str, 1).'\']'; + $result .= '$t->_vars->'.substr($str, 1); } elseif ($type == T_WHITESPACE || in_array($type, $allowed)) { $result .= $str; } else { diff --git a/src/Pluf/Template/ContextVars.php b/src/Pluf/Template/ContextVars.php index cac6741..a5b0f69 100644 --- a/src/Pluf/Template/ContextVars.php +++ b/src/Pluf/Template/ContextVars.php @@ -30,7 +30,12 @@ class Pluf_Template_ContextVars extends ArrayObject { return (isset($this[$prop])) ? $this[$prop] : ''; } - + + function __set($prop, $value) + { + $this[$prop] = $value; + } + function __toString() { return var_export($this, true);