]> git.andy128k.dev Git - ipf-template.git/commitdiff
move template tags to environment
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 17:28:15 +0000 (20:28 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 17:28:15 +0000 (20:28 +0300)
ipf/template/compiler.php
ipf/template/environment.php
ipf/template/tag.php

index 9b648ef0b076a7ae04eec8062ec0c01680718e9e..920320f576b93daf8020a8bf0c766baaca7bf891 100644 (file)
@@ -53,12 +53,6 @@ class IPF_Template_Compiler
 
     public $_usedModifiers = array();
 
-    protected $_allowedTags = array(
-        'url' => 'IPF_Template_Tag_Url',
-    );
-
-    protected $_extraTags = array();
-
     protected $_blockStack = array();
 
     protected $_transStack = array();
@@ -72,15 +66,9 @@ class IPF_Template_Compiler
 
     function __construct($templateContent, $environment)
     {
-        $allowedtags = IPF::get('template_tags', array());
-        $this->_allowedTags = array_merge($allowedtags, $this->_allowedTags);
         $modifiers = IPF::get('template_modifiers', array());
         $this->_modifier = array_merge($modifiers, $this->_modifier);
 
-        foreach ($this->_allowedTags as $name=>$model) {
-            $this->_extraTags[$name] = new $model();
-        }
-
         $this->templateContent = $templateContent;
         $this->environment = $environment;
     }
@@ -399,42 +387,28 @@ class IPF_Template_Compiler
             $this->updateModifierStack($_comp);
             break;
         default:
-            $_end = false;
+            $_start = true;
             $oname = $name;
             if (substr($name, 0, 1) == '/') {
-                $_end = true;
+                $_start = false;
                 $name = substr($name, 1);
             }
             // Here we should allow custom blocks.
 
             // Here we start the template tag calls at the template tag
             // {tag ...} is not a block, so it must be a function.
-            if (!isset($this->_allowedTags[$name])) {
+            if (!$this->environment->isTagAllowed($name)) {
                 trigger_error(sprintf(__('The function tag "%s" is not allowed.'), $name), E_USER_ERROR);
             }
-            $argfct = $this->_parseFinal($args, self::$allowedAssign);
             // $argfct is a string that can be copy/pasted in the PHP code
             // but we need the array of args.
-            $res = '';
-            if (isset($this->_extraTags[$name])) {
-                if (false == $_end) {
-                    if (method_exists($this->_extraTags[$name], 'start')) {
-                        $res .= '$_extra_tag = IPF::factory(\''.$this->_allowedTags[$name].'\', $t); $_extra_tag->start('.$argfct.'); ';
-                    }
-                    if (method_exists($this->_extraTags[$name], 'genStart')) {
-                        $res .= $this->_extraTags[$name]->genStart();
-                    }
-                } else {
-                    if (method_exists($this->_extraTags[$name], 'end')) {
-                        $res .= '$_extra_tag = IPF::factory(\''.$this->_allowedTags[$name].'\', $t); $_extra_tag->end('.$argfct.'); ';
-                    }
-                    if (method_exists($this->_extraTags[$name], 'genEnd')) {
-                        $res .= $this->_extraTags[$name]->genEnd();
-                    }
-                }
-            }
-            if ($res == '') {
-                trigger_error(sprintf(__('The function tag "{%s ...}" is not supported.'), $oname), E_USER_ERROR);
+            $argfct = $this->_parseFinal($args, self::$allowedAssign);
+
+            $res = '$_extra_tag = new '.$this->environment->allowedTags[$name].'($t);';
+            if ($_start) {
+                $res .= '$_extra_tag->start('.$argfct.'); ';
+            } else {
+                $res .= '$_extra_tag->end('.$argfct.'); ';
             }
         }
         return $res;
index 0ea8b8e475a8f1aa60a50167b6e88ace31c0c139..90b7b0a9e040c6824ce6a4968c5365a627919814 100644 (file)
@@ -6,26 +6,11 @@ abstract class IPF_Template_Environment
 
     abstract public function getCompiledTemplateName($template);
 
-    private static $defaultEnvironment = null;
+    public $allowedTags = array();
 
-    public static function getDefault()
+    public function isTagAllowed($name)
     {
-        if (!self::$defaultEnvironment) {
-            $dirs = array();
-
-            $projectTemplates = IPF::get('project_path') . '/templates';
-            if (is_dir($projectTemplates))
-                $dirs[] = $projectTemplates;
-
-            foreach (IPF_Project::getInstance()->appList() as $app) {
-                $applicationTemplates = $app->getPath() . 'templates';
-                if (is_dir($applicationTemplates))
-                    $dirs[] = $applicationTemplates;
-            }
-
-            self::$defaultEnvironment = new IPF_Template_Environment_FileSystem($dirs, IPF::get('tmp'));
-        }
-        return self::$defaultEnvironment;
+        return isset($this->allowedTags[$name]);
     }
 }
 
index ed98a3353d0f450b18b89038878be7ab62a84f7a..1a1ced2dc47faa93d686177d916724c3bbf3a705 100644 (file)
@@ -3,8 +3,20 @@
 class IPF_Template_Tag
 {
     protected $context;
+
     function __construct($context=null)
     {
         $this->context = $context;
     }
+
+    // variable list of arguments
+    function start()
+    {
+    }
+
+    // variable list of arguments
+    function end()
+    {
+    }
 }
+