From 70e3764dbc2265b0a7cf3173d6998d2f9357f501 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 30 Jul 2013 20:28:15 +0300 Subject: [PATCH] move template tags to environment --- ipf/template/compiler.php | 46 ++++++++---------------------------- ipf/template/environment.php | 21 +++------------- ipf/template/tag.php | 12 ++++++++++ 3 files changed, 25 insertions(+), 54 deletions(-) diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index 9b648ef..920320f 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -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; diff --git a/ipf/template/environment.php b/ipf/template/environment.php index 0ea8b8e..90b7b0a 100644 --- a/ipf/template/environment.php +++ b/ipf/template/environment.php @@ -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]); } } diff --git a/ipf/template/tag.php b/ipf/template/tag.php index ed98a33..1a1ced2 100644 --- a/ipf/template/tag.php +++ b/ipf/template/tag.php @@ -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() + { + } } + -- 2.49.0