From 120504d133bc32f9c181112b9241792988cf1c7a Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 30 Jul 2013 23:08:56 +0300 Subject: [PATCH] make all modifiers autoloadable --- ipf/template.php | 21 ---------- ipf/template/compiler.php | 35 +++-------------- ipf/template/modifier.php | 82 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 50 deletions(-) create mode 100644 ipf/template/modifier.php diff --git a/ipf/template.php b/ipf/template.php index fd9b27b..0c40fee 100644 --- a/ipf/template.php +++ b/ipf/template.php @@ -36,24 +36,3 @@ abstract class IPF_Template } } -function IPF_Template_dateFormat($date, $format='%b %e, %Y') -{ - if (substr(PHP_OS,0,3) == 'WIN') { - $_win_from = array ('%e', '%T', '%D'); - $_win_to = array ('%#d', '%H:%M:%S', '%m/%d/%y'); - $format = str_replace($_win_from, $_win_to, $format); - } - $date = date('Y-m-d H:i:s', strtotime($date.' GMT')); - return strftime($format, strtotime($date)); -} - -function IPF_Template_timeFormat($time, $format='Y-m-d H:i:s') -{ - return date($format, $time); -} - -function IPF_Template_floatFormat($number, $decimals=2, $dec_point='.', $thousands_sep=',') -{ - return number_format($number, $decimals, $dec_point, $thousands_sep); -} - diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index c258fa9..195408e 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -32,7 +32,7 @@ class IPF_Template_Compiler 'upper' => 'strtoupper', 'lower' => 'strtolower', 'escxml' => 'htmlspecialchars', - 'escape' => 'IPF_Utils::escape', + 'escape' => 'IPF_Template_Modifier::escape', 'strip_tags' => 'strip_tags', 'escurl' => 'rawurlencode', 'capitalize' => 'ucwords', @@ -43,16 +43,15 @@ class IPF_Template_Compiler 'trim' => 'trim', 'unsafe' => 'IPF_Template_SafeString::markSafe', 'safe' => 'IPF_Template_SafeString::markSafe', - 'date' => 'IPF_Template_dateFormat', - 'time' => 'IPF_Template_timeFormat', - 'floatformat' => 'IPF_Template_floatFormat', - 'limit_words' => 'IPF_Utils::limitWords', + 'date' => 'IPF_Template_Modifier::dateFormat', + 'time' => 'IPF_Template_Modifier::timeFormat', + 'floatformat' => 'IPF_Template_Modifier::floatFormat', + 'limit_words' => 'IPF_Template_Modifier::limitWords', + 'limit_chars' => 'IPF_Template_Modifier::limitCharacters', ); protected $_literals; - public $_usedModifiers = array(); - protected $_blockStack = array(); protected $_transStack = array(); @@ -95,13 +94,6 @@ class IPF_Template_Compiler public function getCompiledTemplate() { $result = $this->compile(); - if (count($this->_usedModifiers)) { - $code = array(); - foreach ($this->_usedModifiers as $modifier) { - $code[] = 'IPF::loadFunction(\''.$modifier.'\'); '; - } - $result = ''.$result; - } $result = str_replace(array('?>', ''), '', $result); $result = str_replace("?>\n", "?>\n\n", $result); return $result; @@ -156,7 +148,6 @@ class IPF_Template_Compiler $compiler = clone($this); $compiler->templateContent = substr($this->templateContent, $blocks[$i]['start'], $blocks[$i]['finish'] - $blocks[$i]['start']); $_tmp = $compiler->compile(); - $this->updateModifierStack($compiler); if (!isset($this->_extendBlocks[$blockName])) { $this->_extendBlocks[$blockName] = $_tmp; } else { @@ -233,10 +224,6 @@ class IPF_Template_Compiler } else { $res = $modifier.'('.$res.')'; } - - if (!in_array($modifier, $this->_usedModifiers)) { - $this->_usedModifiers[] = $modifier; - } } return $res; } @@ -389,7 +376,6 @@ class IPF_Template_Compiler $includedTemplateContent = $this->environment->loadTemplateFile($argfct); $_comp = new IPF_Template_Compiler($includedTemplateContent, $this->environment); $res = $_comp->compile(); - $this->updateModifierStack($_comp); break; default: $_start = true; @@ -473,15 +459,6 @@ class IPF_Template_Compiler } return $result; } - - protected function updateModifierStack($compiler) - { - foreach ($compiler->_usedModifiers as $_um) { - if (!in_array($_um, $this->_usedModifiers)) { - $this->_usedModifiers[] = $_um; - } - } - } } IPF_Template_Compiler::init(); diff --git a/ipf/template/modifier.php b/ipf/template/modifier.php new file mode 100644 index 0000000..0c837da --- /dev/null +++ b/ipf/template/modifier.php @@ -0,0 +1,82 @@ += $n) { + $out = trim($out); + return (strlen($out) == strlen($str)) ? $out : $out.$end_char; + } + } + } +} + -- 2.49.0