From: Andrey Kutejko Date: Sun, 16 Jun 2013 11:11:29 +0000 (+0300) Subject: load template files via environment X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=02f734689901a380d6eec8cb1c707b733519fe2c;p=ipf-template.git load template files via environment --- diff --git a/ipf/template.php b/ipf/template.php index aae8ebd..22b3e27 100644 --- a/ipf/template.php +++ b/ipf/template.php @@ -22,7 +22,7 @@ class IPF_Template public function compile() { - $compiler = new IPF_Template_Compiler($this->tpl, $this->environment->folders); + $compiler = new IPF_Template_Compiler($this->tpl, $this->environment); return $compiler->getCompiledTemplate(); } diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index e6e56b5..b380131 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -62,7 +62,7 @@ class IPF_Template_Compiler protected $_currentTag; - public $templateFolders = array(); + private $environment; public $templateContent = ''; @@ -70,7 +70,7 @@ class IPF_Template_Compiler public $_extendedTemplate = ''; - function __construct($template_file, $folders=array(), $load=true) + function __construct($template_file, $environment, $load=true) { $allowedtags = IPF::get('template_tags', array()); $this->_allowedTags = array_merge($allowedtags, $this->_allowedTags); @@ -85,9 +85,9 @@ class IPF_Template_Compiler $this->_allowedInExpr = array_merge($this->_vartype, $this->_op); $this->_allowedAssign = array_merge($this->_vartype, $this->_assignOp, $this->_op); - $this->templateFolders = $folders; + $this->environment = $environment; if ($load) { - $this->loadTemplateFile($template_file); + $this->templateContent = $this->environment->loadTemplateFile($template_file); } } @@ -185,7 +185,7 @@ class IPF_Template_Compiler if (strlen($this->_extendedTemplate) > 0) { // The template of interest is now the extended template // as we are not in a base template - $this->loadTemplateFile($this->_extendedTemplate); + $this->templateContent = $this->environment->loadTemplateFile($this->_extendedTemplate); $this->_sourceFile = $this->_extendedTemplate; $this->compileBlocks(); //It will recurse to the base template. } else { @@ -196,22 +196,6 @@ class IPF_Template_Compiler } } - function loadTemplateFile($file) - { - // FIXME: Very small security check, could be better. - if (strpos($file, '..') !== false) { - throw new IPF_Exception(sprintf(__('Template file contains invalid characters: %s'), $file)); - } - foreach ($this->templateFolders as $folder) { - if (file_exists($folder.'/'.$file)) { - $this->templateContent = file_get_contents($folder.'/'.$file); - return; - } - } - // File not found in all the folders. - throw new IPF_Exception(sprintf(__('Template file not found: %s'), $file)); - } - function _callback($matches) { list(,$tag, $firstcar) = $matches; @@ -404,7 +388,7 @@ class IPF_Template_Compiler case 'include': // XXX fixme: Will need some security check, when online editing. $argfct = preg_replace('!^[\'"](.*)[\'"]$!', '$1', $args); - $_comp = new IPF_Template_Compiler($argfct, $this->templateFolders); + $_comp = new IPF_Template_Compiler($argfct, $this->environment); $res = $_comp->compile(); $this->updateModifierStack($_comp); break; diff --git a/ipf/template/environment.php b/ipf/template/environment.php index afa7807..7e66b9a 100644 --- a/ipf/template/environment.php +++ b/ipf/template/environment.php @@ -11,6 +11,20 @@ class IPF_Template_Environment $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);