]> git.andy128k.dev Git - ipf-template.git/commitdiff
string templates
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 12:26:09 +0000 (15:26 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 12:26:09 +0000 (15:26 +0300)
ipf/template.php
ipf/template/compiler.php
ipf/template/context.php
ipf/template/environment.php
ipf/template/environment/filesystem.php [new file with mode: 0644]
ipf/template/file.php [new file with mode: 0644]
ipf/template/string.php [new file with mode: 0644]

index 22b3e27caa0e1a4dcf53c633e235dafc797ef1de..8ccf7f2953646ab0faf3ea2ce6b57275fd99993d 100644 (file)
@@ -1,28 +1,21 @@
 <?php
 
-class IPF_Template
+abstract class IPF_Template
 {
-    private $tpl;
-    private $environment;
+    protected $environment;
 
-    function __construct($template, $environment=null)
+    public function __construct($environment)
     {
-        $this->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();
     }
 
index b38013182cffd7aabc28f1ebb2423958abbc2f3f..d7cacc27c713a386cff8c342dd5476578e519f90 100644 (file)
@@ -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;
index 53818e373456230b656119b7d667889167571c84..1973bb63db7c9d0913051bc43a22f97f32ede6cd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-class IPF_Template_Context 
+class IPF_Template_Context
 {
     public $_vars;
 
@@ -22,3 +22,4 @@ class IPF_Template_Context
         $this->_vars[$var] = $value;
     }
 }
+
index 7e66b9a4abf387fc567c8eea8bdb30063dae38af..7d3d79a7a03450807e5f2cbf140ee0b10703c561 100644 (file)
@@ -1,64 +1,17 @@
 <?php
 
-class IPF_Template_Environment
+abstract class IPF_Template_Environment
 {
-    public $folders = array();
-    public $cache = '';
+    abstract public function loadTemplateFile($filename);
 
-    public function __construct($folders, $cache)
-    {
-        $this->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 (file)
index 0000000..5f053ed
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+class IPF_Template_Environment_FileSystem extends IPF_Template_Environment
+{
+    public $folders = array();
+    public $cache = '';
+
+    public function __construct($folders, $cache)
+    {
+        $this->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 (file)
index 0000000..50e0144
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+class IPF_Template_File extends IPF_Template
+{
+    private $tpl;
+
+    function __construct($template, $environment=null)
+    {
+        parent::__construct($environment ? $environment : IPF_Template_Environment::getDefault());
+        $this->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 (file)
index 0000000..09a6f5f
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+class IPF_Template_String extends IPF_Template
+{
+    private $tpl;
+
+    function __construct($template, $environment=null)
+    {
+        parent::__construct($environment ? $environment : IPF_Template_Environment::getDefault());
+        $this->tpl = $template;
+    }
+
+    public function __toString()
+    {
+        return $this->tpl;
+    }
+
+    protected function content()
+    {
+        return $this->tpl;
+    }
+}
+