]> git.andy128k.dev Git - ipf-template.git/commitdiff
make all modifiers autoloadable
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 20:08:56 +0000 (23:08 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 20:08:56 +0000 (23:08 +0300)
ipf/template.php
ipf/template/compiler.php
ipf/template/modifier.php [new file with mode: 0644]

index fd9b27b8a1de537c4ae1154297d15762e082e936..0c40fee69244fc23fa34735a748f0da61f2f6bdc 100644 (file)
@@ -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);
-}
-
index c258fa936df23f5537bf6835518e1239dd8ee7fa..195408e1438ebe663fd1e0188a1ff86f83e6d384 100644 (file)
@@ -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 = '<?php '.implode("\n", $code).'?>'.$result;
-        }
         $result = str_replace(array('?><?php', '<?php ?>', '<?php  ?>'), '', $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 (file)
index 0000000..0c837da
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+final class IPF_Template_Modifier
+{
+    public static function escape($string)
+    {
+        return htmlspecialchars((string)$string, ENT_COMPAT, 'UTF-8');
+    }
+
+    public static function 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));
+    }
+
+    public static function timeFormat($time, $format='Y-m-d H:i:s')
+    {
+        return date($format, $time);
+    }
+
+    public static function floatFormat($number, $decimals=2, $dec_point='.', $thousands_sep=',')
+    {
+        return number_format($number, $decimals, $dec_point, $thousands_sep);
+    }
+
+    /**
+     * Word Limiter
+     *
+     * Limits a string to X number of words.
+     *
+     * @param  string
+     * @param  integer
+     * @param  string  the end character. Usually an ellipsis
+     * @return string
+     */
+    public static function limitWords($str, $limit=100, $end_char='&#8230;')
+    {
+        if (trim($str) == '')
+            return $str;
+        preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
+        if (strlen($str) == strlen($matches[0]))
+            $end_char = '';
+        return rtrim($matches[0]).$end_char;
+    }
+
+    /**
+     * Character Limiter
+     *
+     * Limits the string based on the character count.  Preserves complete words
+     * so the character count may not be exactly as specified.
+     *
+     * @param  string
+     * @param  integer
+     * @param  string  the end character. Usually an ellipsis
+     * @return string
+     */
+    function limitCharacters($str, $n=500, $end_char='&#8230;')
+    {
+        if (strlen($str) < $n)
+            return $str;
+
+        $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
+
+        if (strlen($str) <= $n)
+            return $str;
+
+        $out = "";
+        foreach (explode(' ', trim($str)) as $val) {
+            $out .= $val.' ';
+            if (strlen($out) >= $n) {
+                $out = trim($out);
+                return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
+            }
+        }
+    }
+}
+