]> git.mxchange.org Git - friendica.git/commitdiff
template engine rework
authorFabrixxm <fabrix.xm@gmail.com>
Wed, 27 Mar 2013 14:37:59 +0000 (10:37 -0400)
committerFabrixxm <fabrix.xm@gmail.com>
Tue, 23 Apr 2013 07:27:52 +0000 (03:27 -0400)
- use smarty3 as default engine
- new pluggable template engine system

boot.php
include/friendica_smarty.php
include/template_processor.php
include/text.php
object/TemplateEngine.php [new file with mode: 0644]

index 0475f6ab39ecbc5a77b6ee5e76dc99bdd9f8ea86..477b8331c00644b11909fcb0c150d932ef74eaf3 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -385,6 +385,11 @@ if(! class_exists('App')) {
                        'stylesheet' => '',
                        'template_engine' => 'internal',
                );
+               
+               // array of registered template engines ('name'=>'class name')
+               public $template_engines = array();
+               // array of instanced template engines ('name'=>'instance')
+               public $template_engine_instance = array();
 
                private $ldelim = array(
                        'internal' => '',
@@ -539,6 +544,17 @@ if(! class_exists('App')) {
                        $mobile_detect = new Mobile_Detect();
                        $this->is_mobile = $mobile_detect->isMobile();
                        $this->is_tablet = $mobile_detect->isTablet();
+                       
+                       /**
+                        * register template engines
+                        */
+                       $dc = get_declared_classes();
+                       foreach ($dc as $k) {
+                               if (in_array("ITemplateEngine", class_implements($k))){
+                                       $this->register_template_engine($k);
+                               }
+                       }
+                       
                }
 
                function get_basepath() {
@@ -712,13 +728,63 @@ if(! class_exists('App')) {
                        return $this->cached_profile_image[$avatar_image];
                }
 
+
+               /**
+                * register template engine class
+                * if $name is "", is used class static property $class::$name
+                * @param string $class
+                * @param string $name
+                */
+               function register_template_engine($class, $name = '') {
+                       if ($name===""){
+                               $v = get_class_vars( $class );
+                               if(x($v,"name")) $name = $v['name'];
+                       }
+                       if ($name===""){
+                               echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
+                               killme(); 
+                       }
+                       $this->template_engines[$name] = $class;
+               }
+
+               /**
+                * return template engine instance. If $name is not defined,
+                * return engine defined by theme, or default
+                * 
+                * @param strin $name Template engine name
+                * @return object Template Engine instance
+                */
+               function template_engine($name = ''){
+                       
+                       if ($name!=="") {
+                               $template_engine = $name;
+                       } else {
+                               $template_engine = 'smarty3';
+                               if (x($this->theme, 'template_engine')) {
+                                       $template_engine = $this->theme['template_engine'];
+                               }
+                       }
+                       if (isset($this->template_engines[$template_engine])){
+                               if(isset($this->template_engine_instance[$template_engine])){
+                                       return $this->template_engine_instance[$template_engine];
+                               } else {
+                                       $class = $this->template_engines[$template_engine];
+                                       $obj = new $class;
+                                       $this->template_engine_instance[$template_engine] = $obj;
+                                       return $obj;
+                               }
+                       }
+                       
+                       echo "template engine <tt>$template_engine</tt> is not registered!\n"; killme();
+               }
+
                function get_template_engine() {
                        return $this->theme['template_engine'];
                }
 
-               function set_template_engine($engine = 'internal') {
+               function set_template_engine($engine = 'smarty3') {
 
-                       $this->theme['template_engine'] = 'internal';
+                       $this->theme['template_engine'] = 'smarty3';
 
                        switch($engine) {
                                case 'smarty3':
@@ -730,11 +796,11 @@ if(! class_exists('App')) {
                        }
                }
 
-               function get_template_ldelim($engine = 'internal') {
+               function get_template_ldelim($engine = 'smarty3') {
                        return $this->ldelim[$engine];
                }
 
-               function get_template_rdelim($engine = 'internal') {
+               function get_template_rdelim($engine = 'smarty3') {
                        return $this->rdelim[$engine];
                }
 
index b3f0d18a01d32509f1ca8345c6f0403eff148d51..f9d91a827d01e58e4bb83985d1dbace330e3fc24 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 
+require_once "object/TemplateEngine.php";
 require_once("library/Smarty/libs/Smarty.class.php");
 
 class FriendicaSmarty extends Smarty {
-
        public $filename;
 
        function __construct() {
@@ -37,7 +37,33 @@ class FriendicaSmarty extends Smarty {
                }
                return $this->fetch('file:' . $this->filename);
        }
-}
-
+       
 
+}
 
+class FriendicaSmartyEngine implements ITemplateEngine {
+       static $name ="smarty3";
+       // ITemplateEngine interface
+       public function replace_macros($s, $r) {
+               $template = '';
+               if(gettype($s) === 'string') {
+                       $template = $s;
+                       $s = new FriendicaSmarty();
+               }
+               foreach($r as $key=>$value) {
+                       if($key[0] === '$') {
+                               $key = substr($key, 1);
+                       }
+                       $s->assign($key, $value);
+               }
+               return $s->parsed($template);           
+       }
+       
+       public function get_template_file($file, $root=''){
+               $a = get_app();
+               $template_file = get_template_file($a, 'smarty3/' . $file, $root);
+               $template = new FriendicaSmarty();
+               $template->filename = $template_file;
+               return $template;
+       }
+}
index ebc03b8d84404907e350ec4367bf19e9d40a0299..49d37488f9f54844e5539bbac39cc5be7ca31ca5 100644 (file)
@@ -1,9 +1,11 @@
 <?php
+require_once 'object/TemplateEngine.php';
 
 define("KEY_NOT_EXISTS", '^R_key_not_Exists^');
 
-class Template {
-
+class Template implements ITemplateEngine {
+       static $name ="internal";
+       
        var $r;
        var $search;
        var $replace;
@@ -256,7 +258,8 @@ class Template {
                return $s;
        }
 
-       public function replace($s, $r) {
+       // TemplateEngine interface
+       public function replace_macros($s, $r) {
                $this->r = $r;
 
                // remove comments block
@@ -276,12 +279,18 @@ class Template {
                        $count++;
                        $s = $this->var_replace($s);
                }
-               return $s;
+               return template_unescape($s);
        }
-
+       
+       public function get_template_file($file, $root='') {
+               $a = get_app();
+               $template_file = get_template_file($a, $file, $root);
+               $content = file_get_contents($template_file);
+               return $content;                
+       }
+       
 }
 
-$t = new Template;
 
 function template_escape($s) {
 
index 3d244c61ffcb706de6d662ecf20ad411d24d6bc5..628b4fc2daaa4be33ba7997863275b888d1ad6d1 100644 (file)
@@ -15,39 +15,20 @@ if(! function_exists('replace_macros')) {
 /**
  * This is our template processor
  * 
- * @global Template $t
  * @param string|FriendicaSmarty $s the string requiring macro substitution, 
  *                                                                     or an instance of FriendicaSmarty
  * @param array $r key value pairs (search => replace)
  * @return string substituted string
  */
 function replace_macros($s,$r) {
-       global $t;
        
        $stamp1 = microtime(true);
 
        $a = get_app();
 
-       if($a->theme['template_engine'] === 'smarty3') {
-               $template = '';
-               if(gettype($s) === 'string') {
-                       $template = $s;
-                       $s = new FriendicaSmarty();
-               }
-               foreach($r as $key=>$value) {
-                       if($key[0] === '$') {
-                               $key = substr($key, 1);
-                       }
-                       $s->assign($key, $value);
-               }
-               $output = $s->parsed($template);
-       }
-       else {
-               $r =  $t->replace($s,$r);
+       $t = $a->template_engine();
+       $output = $t->replace_macros($s,$r);
 
-               $output = template_unescape($r);
-       }
-       $a = get_app();
        $a->save_timestamp($stamp1, "rendering");
 
        return $output;
@@ -582,6 +563,14 @@ function get_markup_template($s, $root = '') {
        $stamp1 = microtime(true);
 
        $a = get_app();
+       $t = $a->template_engine();
+       
+       $template = $t->get_template_file($s, $root);
+       
+       $a->save_timestamp($stamp1, "file");
+       
+       return $template;
+       /*
 
        if($a->theme['template_engine'] === 'smarty3') {
                $template_file = get_template_file($a, 'smarty3/' . $s, $root);
@@ -602,6 +591,7 @@ function get_markup_template($s, $root = '') {
                return $content;
 
        }
+        */
 }}
 
 if(! function_exists("get_template_file")) {
diff --git a/object/TemplateEngine.php b/object/TemplateEngine.php
new file mode 100644 (file)
index 0000000..cbd74aa
--- /dev/null
@@ -0,0 +1,11 @@
+<?php\r
+require_once 'boot.php';\r
+\r
+\r
+/**\r
+ * Interface for template engines\r
+ */\r
+interface ITemplateEngine {\r
+       public function replace_macros($s,$v);\r
+       public function get_template_file($file, $root='');\r
+}\r