/** * modifier plugin : Truncate a string. * * Truncate a string to a certain length if necessary, optionally splitting in * the middle of a word, and appending the $etc string. * <pre>{$mytext|truncate} * {$mytext|truncate:40} * {$mytext|truncate:45:'...'} * {$mytext|truncate:60:'...':true} * </pre> * * @param \Jelix\Castor\CastorCore $tpl The template * @param string $string the string to truncate * @param int $length the number of char to keep * @param string $etc the string to append to the truncated string * @param bool $break_words false if the last word shouldn't be cut * * @return string */ function jtpl_modifier2_common_truncate(\Jelix\Castor\CastorCore $tpl, $string, $length = 80, $etc = '...', $break_words = false) { if (function_exists('mb_strlen')) { $f_strlen = 'mb_strlen'; } else { $f_strlen = 'iconv_strlen'; } if (function_exists('mb_substr')) { $f_substr = 'mb_substr'; } else { $f_substr = 'iconv_substr'; } if ($length == 0) { return ''; } $charset = $tpl->getEncoding(); if ($f_strlen($string, $charset) > $length) { $length -= $f_strlen($etc, $charset); if (!$break_words) { $string = preg_replace('/\\s+?(\\S+)?$/', '', $f_substr($string, 0, $length + 1, $charset)); } return $f_substr($string, 0, $length, $charset) . $etc; } else { return $string; } }
/** * Modifier plugin : count the number of characters in a text. * * <pre>{$mytext|count_characters} * {$mytext|count_characters:true}</pre> * * @param \Jelix\Castor\CastorCore $tpl The template * @param string $string * @param bool $include_spaces include whitespace in the character count * * @return int */ function jtpl_modifier2_common_count_characters(\Jelix\Castor\CastorCore $tpl, $string, $include_spaces = false) { if ($include_spaces) { return iconv_strlen($string, $tpl->getEncoding()); } return preg_match_all("/[^\\s]/", $string, $match); }
/** * Type: function<br> * Name: cycle<br> * Date: Feb, 2008<br> * Purpose: cycle through an array, a given cycle name or the default one<br> * Input: * - param = array of values or name of cycle to cycle (optional). * * Examples:<br> * <pre> * {cycle array('aa','bb','cc')} * {cycle 'name'} * {cycle} * </pre> * * @param \Jelix\Castor\CastorCore $tpl * @param string,array $param the name of the cycle, or the list of values * * @return string */ function jtpl_function_common_cycle(\Jelix\Castor\CastorCore $tpl, $param = '') { if (is_array($param)) { static $cycle_vars; if (!isset($cycle_vars['values'])) { $cycle_vars['values'] = $param; $cycle_vars['index'] = 0; } $retval = $cycle_vars['values'][$cycle_vars['index']]; if ($cycle_vars['index'] >= count($cycle_vars['values']) - 1) { $cycle_vars['index'] = 0; } else { ++$cycle_vars['index']; } } else { $cycle_name = $param ? $param : 'default'; if (isset($tpl->_privateVars['cycle'][$cycle_name]['values'])) { $cycle_array = $tpl->_privateVars['cycle'][$cycle_name]['values']; } else { throw $tpl->getInternalException('errors.tplplugin.function.argument.unknown', array($cycle_name, 'cycle', '')); } $index =& $tpl->_privateVars['cycle'][$cycle_name]['index']; $retval = $cycle_array[$index]; if ($index >= count($cycle_array) - 1) { $index = 0; } else { ++$index; } } echo $retval; }
/** * Type: function<br> * Name: cycle_init<br> * Date: Feb, 2008<br> * Purpose: initialize cycling through given values<br> * Input: * - values = comma separated list of values to cycle * - name = name of cycle (optional). * * Examples:<br> * <pre> * {cycle_init '#eeeeee,#d0d0d0d'} * {cycle_init 'name','#eeeeee,#d0d0d0d'} * </pre> * * @param \Jelix\Castor\CastorCore $tpl * @param string $name the name of the cycle or the list of values * @param string $values the list of values * * @return 1 */ function jtpl_function_common_cycle_init(\Jelix\Castor\CastorCore $tpl, $name, $values = '') { if ($name == '') { throw $tpl->getInternalException('errors.tplplugin.cfunction.bad.argument.number', array('cycle_init', '1', '')); } if (is_array($name)) { $values = $name; $name = 'default'; } elseif (strpos($name, ',') === false) { if ($values == '') { throw $tpl->getInternalException('errors.tplplugin.cfunction.bad.argument.number', array('cycle_init', '2', '')); } if (!is_array($values)) { if (strpos($values, ',') === false) { throw $tpl->getInternalException('errors.tplplugin.function.invalid', array('cycle_init', '', '')); } $values = explode(',', $values); } } else { $values = explode(',', $name); $name = 'default'; } $tpl->_privateVars['cycle'][$name]['values'] = $values; $tpl->_privateVars['cycle'][$name]['index'] = 0; }
/** * modifier plugin : regular epxression search/replace. * * You should provide two arguments, like the first both of preg_replace * {$mystring|regex_replace:'/(\w+) (\d+), (\d+)/i':'${1}1,$3'} * * @param \Jelix\Castor\CastorCore $tpl The template * @param string $string * @param string|array $search * @param string|array $replace * * @return string */ function jtpl_modifier2_common_regex_replace(\Jelix\Castor\CastorCore $tpl, $string, $search, $replace) { if (preg_match('!\\W(\\w+)$!s', $search, $match) && strpos($match[1], 'e') !== false) { /* remove eval-modifier from $search */ $search = substr($search, 0, -iconv_strlen($match[1], $tpl->getEncoding())) . str_replace('e', '', $match[1]); } return preg_replace($search, $replace, $string); }
/** * Type: function<br> * Name: cycle_reset<br> * Date: Feb, 2008<br> * Purpose: reset the cycle of a given cycle name or the default<br> * Input: * - name = name of cycle (optional). * * Examples:<br> * <pre> * {cycle_reset 'name'} * {cycle_reset} * </pre> * * @param \Jelix\Castor\CastorCore $tpl * @param string $name the name of the cycle * * @return 1 */ function jtpl_function_common_cycle_reset(\Jelix\Castor\CastorCore $tpl, $name = '') { $cycle_name = $name ? $name : 'default'; if (isset($tpl->_privateVars['cycle'][$cycle_name])) { $tpl->_privateVars['cycle'][$cycle_name]['index'] = 0; } else { throw $tpl->getInternalException('errors.tplplugin.function.argument.unknown', array($cycle_name, 'cycle', '')); } }
public function __construct() { $config = jApp::config(); $this->_vars['j_basepath'] = $config->urlengine['basePath']; $this->_vars['j_jelixwww'] = $config->urlengine['jelixWWWPath']; $this->_vars['j_jquerypath'] = $config->urlengine['jqueryPath']; $this->_vars['j_themepath'] = $config->urlengine['basePath'] . 'themes/' . $config->theme . '/'; $this->_vars['j_locale'] = $config->locale; parent::__construct(); }
/** * Examples: * <pre> * {mailto array("address"=>"*****@*****.**")} * {mailto array("address"=>"*****@*****.**","encode"=>"javascript")} * {mailto array("address"=>"*****@*****.**","encode"=>"hex")} * {mailto array("address"=>"*****@*****.**","subject"=>"Hello to you!")} * {mailto array("address"=>"*****@*****.**","cc"=>"you@domain.com,they@domain.com")} * {mailto array("address"=>"*****@*****.**","extra"=>'class="mailto"')} * </pre>. * * @params \Jelix\Castor\CastorCore $tpl * @params array $params */ function jtpl_function_html_mailto(\Jelix\Castor\CastorCore $tpl, $params) { $extra = ''; if (empty($params['address'])) { throw $tpl->getInternalException('errors.tplplugin.function.argument.unknown', array('address', 'mailto', $tpl->_templateName)); } else { $address = $params['address']; } $text = $address; // netscape and mozilla do not decode %40 (@) in BCC field (bug?) // so, don't encode it. $search = array('%40', '%2C'); $replace = array('@', ','); $mail_parms = array(); foreach ($params as $var => $value) { switch ($var) { case 'cc': case 'bcc': case 'followupto': if (!empty($value)) { $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value)); } break; case 'subject': case 'newsgroups': $mail_parms[] = $var . '=' . rawurlencode($value); break; case 'extra': case 'text': ${$var} = $value; default: } } $mail_parm_vals = ''; for ($i = 0; $i < count($mail_parms); ++$i) { $mail_parm_vals .= 0 == $i ? '?' : '&'; $mail_parm_vals .= $mail_parms[$i]; } $address .= $mail_parm_vals; $encode = empty($params['encode']) ? 'none' : $params['encode']; if (!in_array($encode, array('javascript', 'javascript_charcode', 'hex', 'none'))) { throw $tpl->getInternalException('errors.tplplugin.function.argument.unknown', array($encode, 'mailto', $tpl->_templateName)); } if ($encode == 'javascript') { $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; $js_encode = ''; for ($x = 0; $x < strlen($string); ++$x) { $js_encode .= '%' . bin2hex($string[$x]); } echo '<script type="text/javascript">//<![CDATA[ eval(unescape(\'' . $js_encode . '\')); //]]> </script>'; } elseif ($encode == 'javascript_charcode') { $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; for ($x = 0, $y = strlen($string); $x < $y; ++$x) { $ord[] = ord($string[$x]); } $_ret = "<script type=\"text/javascript\">\n"; $_ret .= "//<![CDATA[\n"; $_ret .= '{document.write(String.fromCharCode('; $_ret .= implode(',', $ord); $_ret .= '))'; $_ret .= "}\n"; $_ret .= "//]]>\n"; $_ret .= "</script>\n"; echo $_ret; } elseif ($encode == 'hex') { preg_match('!^(.*)(\\?.*)$!', $address, $match); if (!empty($match[2])) { throw $tpl->getInternalException('errors.tplplugin.function.argument.unknown', array($match[2], ' ', ' ')); } $address_encode = ''; for ($x = 0; $x < strlen($address); ++$x) { if (preg_match('!\\w!', $address[$x])) { $address_encode .= '%' . bin2hex($address[$x]); } else { $address_encode .= $address[$x]; } } $text_encode = ''; for ($x = 0; $x < strlen($text); ++$x) { $text_encode .= '&#x' . bin2hex($text[$x]) . ';'; } $mailto = 'mailto:'; echo '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>'; } else { // no encoding echo '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; } }