> Smarty模板引擎中文在线手册 > Modifiers [修正器]

register_modifier(), unregister_modifier().
也可参考register_modifier(), unregister_modifier().

Example 16-3. simple modifier plugin简单修正器插件

This plugin basically aliases one of the built-in PHP functions. It does not have any additional parameters.
这个插件主要目的是用另一个名字替换一个内置PHP函数的名字。他没有任何多余的参数。

<?php
/*
 * smarty plugin
 * -------------------------------------------------------------
 * File: modifier.capitalize.php
 * Type: modifier
 * Name: capitalize
 * Purpose: capitalize words in the string
 * -------------------------------------------------------------
 */
function smarty_modifier_capitalize($string)
{
 return ucwords($string);
}
?>

Example 16-4. more complex modifier plugin更加复杂的修正器插件

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File: modifier.truncate.php
 * Type: modifier
 * Name: truncate
 * Purpose: Truncate a string to a certain length if necessary,
 * optionally splitting in the middle of a word, and 
 * appending the $etc string.
 * -------------------------------------------------------------
 */
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
 $break_words = false)
{
 if ($length == 0)
 return '';

 if (strlen($string) > $length) {
 $length -= strlen($etc);
 $fragment = substr($string, 0, $length+1);
 if ($break_words)
 $fragment = substr($fragment, 0, -1);
 else
 $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment);
 return $fragment.$etc;
 } else
 return $string;
}
?>

上一篇:
下一篇: