]> git.andy128k.dev Git - ipf-template.git/commitdiff
load template files via environment
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 11:11:29 +0000 (14:11 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 11:11:29 +0000 (14:11 +0300)
ipf/template.php
ipf/template/compiler.php
ipf/template/environment.php

index aae8ebd32d2633ba8c8f29832f6853785dfb6663..22b3e27caa0e1a4dcf53c633e235dafc797ef1de 100644 (file)
@@ -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();
     }
 
index e6e56b5a099a5441c6637a041f67b388ceb0e18e..b38013182cffd7aabc28f1ebb2423958abbc2f3f 100644 (file)
@@ -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;
index afa780715ff8b22a678816d293f13d8c87088ee5..7e66b9a4abf387fc567c8eea8bdb30063dae38af 100644 (file)
@@ -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);