}
}
-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);
-}
-
'upper' => 'strtoupper',
'lower' => 'strtolower',
'escxml' => 'htmlspecialchars',
- 'escape' => 'IPF_Utils::escape',
+ 'escape' => 'IPF_Template_Modifier::escape',
'strip_tags' => 'strip_tags',
'escurl' => 'rawurlencode',
'capitalize' => 'ucwords',
'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();
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;
$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 {
} else {
$res = $modifier.'('.$res.')';
}
-
- if (!in_array($modifier, $this->_usedModifiers)) {
- $this->_usedModifiers[] = $modifier;
- }
}
return $res;
}
$includedTemplateContent = $this->environment->loadTemplateFile($argfct);
$_comp = new IPF_Template_Compiler($includedTemplateContent, $this->environment);
$res = $_comp->compile();
- $this->updateModifierStack($_comp);
break;
default:
$_start = true;
}
return $result;
}
-
- protected function updateModifierStack($compiler)
- {
- foreach ($compiler->_usedModifiers as $_um) {
- if (!in_array($_um, $this->_usedModifiers)) {
- $this->_usedModifiers[] = $_um;
- }
- }
- }
}
IPF_Template_Compiler::init();
--- /dev/null
+<?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='…')
+ {
+ 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='…')
+ {
+ 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;
+ }
+ }
+ }
+}
+