From 87028717db9c999659c7b3908ca07f2f132c4b0f Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 16 Jun 2013 15:26:09 +0300 Subject: [PATCH] string templates --- ipf/template.php | 23 ++++------ ipf/template/compiler.php | 25 ++++------- ipf/template/context.php | 3 +- ipf/template/environment.php | 55 ++---------------------- ipf/template/environment/filesystem.php | 56 +++++++++++++++++++++++++ ipf/template/file.php | 23 ++++++++++ ipf/template/string.php | 23 ++++++++++ 7 files changed, 125 insertions(+), 83 deletions(-) create mode 100644 ipf/template/environment/filesystem.php create mode 100644 ipf/template/file.php create mode 100644 ipf/template/string.php diff --git a/ipf/template.php b/ipf/template.php index 22b3e27..8ccf7f2 100644 --- a/ipf/template.php +++ b/ipf/template.php @@ -1,28 +1,21 @@ tpl = $template; - - if ($environment) - $this->environment = $environment; - else - $this->environment = IPF_Template_Environment::getDefault(); + $this->environment = $environment; } - public function __toString() - { - return $this->tpl; - } + abstract public function __toString(); + + abstract protected function content(); public function compile() { - $compiler = new IPF_Template_Compiler($this->tpl, $this->environment); + $compiler = new IPF_Template_Compiler($this->content(), $this->environment); return $compiler->getCompiledTemplate(); } diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index b380131..d7cacc2 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -58,8 +58,6 @@ class IPF_Template_Compiler protected $_transStack = array(); protected $_transPlural = false; - protected $_sourceFile; - protected $_currentTag; private $environment; @@ -68,9 +66,7 @@ class IPF_Template_Compiler public $_extendBlocks = array(); - public $_extendedTemplate = ''; - - function __construct($template_file, $environment, $load=true) + function __construct($templateContent, $environment) { $allowedtags = IPF::get('template_tags', array()); $this->_allowedTags = array_merge($allowedtags, $this->_allowedTags); @@ -80,15 +76,13 @@ class IPF_Template_Compiler foreach ($this->_allowedTags as $name=>$model) { $this->_extraTags[$name] = new $model(); } - $this->_sourceFile = $template_file; $this->_allowedInVar = array_merge($this->_vartype, $this->_op); $this->_allowedInExpr = array_merge($this->_vartype, $this->_op); $this->_allowedAssign = array_merge($this->_vartype, $this->_assignOp, $this->_op); + + $this->templateContent = $templateContent; $this->environment = $environment; - if ($load) { - $this->templateContent = $this->environment->loadTemplateFile($template_file); - } } function compile() @@ -159,10 +153,10 @@ class IPF_Template_Compiler function compileBlocks() { $tplcontent = $this->templateContent; - $this->_extendedTemplate = ''; + $extendedTemplate = ''; // Match extends on the first line of the template if (preg_match("!{extends\s['\"](.*?)['\"]}!", $tplcontent, $_match)) { - $this->_extendedTemplate = $_match[1]; + $extendedTemplate = $_match[1]; } // Get the blocks in the current template $blocks = self::toplevelBlocks($this->templateContent); @@ -182,11 +176,10 @@ class IPF_Template_Compiler } } } - if (strlen($this->_extendedTemplate) > 0) { + if (strlen($extendedTemplate) > 0) { // The template of interest is now the extended template // as we are not in a base template - $this->templateContent = $this->environment->loadTemplateFile($this->_extendedTemplate); - $this->_sourceFile = $this->_extendedTemplate; + $this->templateContent = $this->environment->loadTemplateFile($extendedTemplate); $this->compileBlocks(); //It will recurse to the base template. } else { // Replace the current blocks by a place holder @@ -386,9 +379,9 @@ class IPF_Template_Compiler $res = '$_b_t_s=ob_get_contents(); ob_end_clean(); ob_start(); '; break; case 'include': - // XXX fixme: Will need some security check, when online editing. $argfct = preg_replace('!^[\'"](.*)[\'"]$!', '$1', $args); - $_comp = new IPF_Template_Compiler($argfct, $this->environment); + $includedTemplateContent = $this->environment->loadTemplateFile($argfct); + $_comp = new IPF_Template_Compiler($includedTemplateContent, $this->environment); $res = $_comp->compile(); $this->updateModifierStack($_comp); break; diff --git a/ipf/template/context.php b/ipf/template/context.php index 53818e3..1973bb6 100644 --- a/ipf/template/context.php +++ b/ipf/template/context.php @@ -1,6 +1,6 @@ _vars[$var] = $value; } } + diff --git a/ipf/template/environment.php b/ipf/template/environment.php index 7e66b9a..7d3d79a 100644 --- a/ipf/template/environment.php +++ b/ipf/template/environment.php @@ -1,64 +1,17 @@ folders = $folders; - $this->cache = $cache; - } - - public function loadTemplateFile($filename) - { - // FIXME: Very small security check, could be better. - if (strpos($filename, '..') !== false) { - throw new IPF_Exception(sprintf(__('Template file contains invalid characters: %s'), $filename)); - } - foreach ($this->folders as $folder) { - if (file_exists($folder.'/'.$filename)) { - return file_get_contents($folder.'/'.$filename); - } - } - throw new IPF_Exception(sprintf(__('Template file not found: %s'), $filename)); - } - - public function getCompiledTemplateName($template) - { - $_tmp = var_export($this->folders, true); - $filename = $this->cache.'/IPF_Template-'.md5($_tmp.(string)$template).'.phps'; - if (IPF::get('debug') or !file_exists($filename)) { - $this->write($filename, $template->compile()); - } - return $filename; - } - - private function write($filename, $content) - { - $fp = @fopen($filename, 'a'); - if ($fp !== false) { - flock($fp, LOCK_EX); - ftruncate($fp, 0); - rewind($fp); - fwrite($fp, $content, strlen($content)); - flock($fp, LOCK_UN); - fclose($fp); - @chmod($filename, 0777); - return true; - } else { - throw new IPF_Exception_Template(sprintf(__('Cannot write the compiled template: %s'), $filename)); - } - return false; - } + abstract public function getCompiledTemplateName($template); private static $defaultEnvironment = null; public static function getDefault() { if (!self::$defaultEnvironment) - self::$defaultEnvironment = new IPF_Template_Environment(IPF::get('template_dirs'), IPF::get('tmp')); + self::$defaultEnvironment = new IPF_Template_Environment_FileSystem(IPF::get('template_dirs'), IPF::get('tmp')); return self::$defaultEnvironment; } } diff --git a/ipf/template/environment/filesystem.php b/ipf/template/environment/filesystem.php new file mode 100644 index 0000000..5f053ed --- /dev/null +++ b/ipf/template/environment/filesystem.php @@ -0,0 +1,56 @@ +folders = $folders; + $this->cache = $cache; + } + + public function loadTemplateFile($filename) + { + // FIXME: Very small security check, could be better. + if (strpos($filename, '..') !== false) { + throw new IPF_Exception(sprintf(__('Template file contains invalid characters: %s'), $filename)); + } + foreach ($this->folders as $folder) { + if (file_exists($folder.'/'.$filename)) { + return file_get_contents($folder.'/'.$filename); + } + } + throw new IPF_Exception(sprintf(__('Template file not found: %s'), $filename)); + } + + public function getCompiledTemplateName($template) + { + $_tmp = var_export($this->folders, true); + $filename = $this->cache.'/IPF_Template-'.md5($_tmp.(string)$template).'.phps'; + if (IPF::get('debug') or !file_exists($filename)) { + $this->write($filename, $template->compile()); + } + return $filename; + } + + private function write($filename, $content) + { + $fp = @fopen($filename, 'a'); + if ($fp !== false) { + flock($fp, LOCK_EX); + ftruncate($fp, 0); + rewind($fp); + fwrite($fp, $content, strlen($content)); + flock($fp, LOCK_UN); + fclose($fp); + @chmod($filename, 0777); + return true; + } else { + throw new IPF_Exception_Template(sprintf(__('Cannot write the compiled template: %s'), $filename)); + } + return false; + } +} + diff --git a/ipf/template/file.php b/ipf/template/file.php new file mode 100644 index 0000000..50e0144 --- /dev/null +++ b/ipf/template/file.php @@ -0,0 +1,23 @@ +tpl = $template; + } + + public function __toString() + { + return $this->tpl; + } + + protected function content() + { + return $this->environment->loadTemplateFile($this->tpl); + } +} + diff --git a/ipf/template/string.php b/ipf/template/string.php new file mode 100644 index 0000000..09a6f5f --- /dev/null +++ b/ipf/template/string.php @@ -0,0 +1,23 @@ +tpl = $template; + } + + public function __toString() + { + return $this->tpl; + } + + protected function content() + { + return $this->tpl; + } +} + -- 2.49.0