HBcms是一个用Pear框架+Smarty模板引擎构架的cms项目,欢迎大家参与制作Smarty模板

truncate
截取

Parameter Position参数位置 Type参数类型 Required必需 Default默认 Description描述
1 integer No 80 This determines how many characters to truncate to.
截取字符的数量
2 string No ... This is the text to append if truncation occurs.
截取后追加在截取词后面的字符串
3 boolean No false This determines whether or not to truncate at a word boundary (false), or at the exact character (true).
是截取到词的边界(假)还是精确到字符(真)

This truncates a variable to a character length, default is 80.
As an optional second parameter, you can specify a string of text to display at the end if the variable was truncated. The characters in the string are included with the original truncation length.
By default, truncate will attempt to cut off at a word boundary.
If you want to cut off at the exact character length, pass the optional third parameter of true.

从字符串开始处截取某长度的字符.默认是80个.
你也可以指定第二个参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。
默认情况下,smarty会截取到一个词的末尾。
如果你想要精确的截取多少个字符,把第三个参数改为"true"

Example 5-21. truncate
例5-21.截取

index.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');

index.tpl:

{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}

输出结果:

Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
这是一份简单的Smarty手册和Smarty教材,熟练掌握Smarty模板,让您做项目事半功倍