]> git.andy128k.dev Git - ipf-template.git/commitdiff
separate template and environment
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 10:59:11 +0000 (13:59 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 16 Jun 2013 10:59:11 +0000 (13:59 +0300)
ipf/template.php
ipf/template/environment.php [new file with mode: 0644]

index c8828dc745c310f7883a9a917533979b6eadab5c..aae8ebd32d2633ba8c8f29832f6853785dfb6663 100644 (file)
@@ -2,45 +2,37 @@
 
 class IPF_Template
 {
-    public $tpl = '';
-    public $folders = array();
-    public $cache = '';
-    public $compiled_template = '';
-    public $template_content = '';
-    public $context = null;
+    private $tpl;
+    private $environment;
 
-    function __construct($template, $folders=null, $cache=null)
+    function __construct($template, $environment=null)
     {
         $this->tpl = $template;
-        if (is_null($folders)) {
-            $this->folders = IPF::get('template_dirs');
-        } else {
-            $this->folders = $folders;
-        }
-        if (is_null($cache)) {
-            $this->cache = IPF::get('tmp');
-        } else {
-            $this->cache = $cache;
-        }
 
+        if ($environment)
+            $this->environment = $environment;
+        else
+            $this->environment = IPF_Template_Environment::getDefault();
     }
 
-    function render($c=null)
+    public function __toString()
     {
-        $this->compiled_template = $this->getCompiledTemplateName();
-        if (!file_exists($this->compiled_template) or IPF::get('debug')) {
-            $compiler = new IPF_Template_Compiler($this->tpl, $this->folders);
-            $this->template_content = $compiler->getCompiledTemplate();
-            $this->write();
-        }
-        if (is_null($c)) {
-            $c = new IPF_Template_Context();
-        }
-        $this->context = $c;
+        return $this->tpl;
+    }
+
+    public function compile()
+    {
+        $compiler = new IPF_Template_Compiler($this->tpl, $this->environment->folders);
+        return $compiler->getCompiledTemplate();
+    }
+
+    public function render($c=null)
+    {
+        $compiled_template = $this->environment->getCompiledTemplateName($this);
         ob_start();
         $t = $c;
         try {
-            include $this->compiled_template;
+            include $compiled_template;
         } catch (Exception $e) {
             ob_clean();
             throw $e;
@@ -49,31 +41,6 @@ class IPF_Template
         ob_end_clean();
         return $a;
     }
-
-    function getCompiledTemplateName()
-    {
-        $_tmp = var_export($this->folders, true);
-        return $this->cache.'/IPF_Template-'.md5($_tmp.$this->tpl).'.phps';
-    }
-
-    function write()
-    {
-        $fp = @fopen($this->compiled_template, 'a');
-        if ($fp !== false) {
-            flock($fp, LOCK_EX);
-            ftruncate($fp, 0);
-            rewind($fp);
-            fwrite($fp, $this->template_content, strlen($this->template_content));
-            flock($fp, LOCK_UN);
-            fclose($fp);
-            @chmod($this->compiled_template, 0777);
-            return true;
-        } else {
-            throw new IPF_Exception_Template(sprintf(__('Cannot write the compiled template: %s'), $this->compiled_template));
-        }
-        return false;
-    }
-
 }
 
 function IPF_Template_unsafe($string)
@@ -121,3 +88,4 @@ function IPF_Template_safeEcho($mixed, $echo=true)
         }
     }
 }
+
diff --git a/ipf/template/environment.php b/ipf/template/environment.php
new file mode 100644 (file)
index 0000000..afa7807
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+class IPF_Template_Environment
+{
+    public $folders = array();
+    public $cache = '';
+
+    public function __construct($folders, $cache)
+    {
+        $this->folders = $folders;
+        $this->cache = $cache;
+    }
+
+    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;
+    }
+
+    private static $defaultEnvironment = null;
+
+    public static function getDefault()
+    {
+        if (!self::$defaultEnvironment)
+            self::$defaultEnvironment = new IPF_Template_Environment(IPF::get('template_dirs'), IPF::get('tmp'));
+        return self::$defaultEnvironment;
+    }
+}
+