What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
截取字符串到指定长度,默认长度是80.
第二个参数可选,指定了截取后代替显示的字符。
截取后的字符长度是截取规定的长度加上第二个参数的字符长度。
默认truncate
会尝试按单词进行截取。
如果你希望按字符截取(单词可能会被截断),需要设置第三个参数TRUE
。
参数顺序 | 类型 | 必选参数 | 默认值 | 说明 |
---|---|---|---|---|
1 | integer | No | 80 | 截取的长度 |
2 | string | No | ... | 截取后替代显示的字符,该字符长度会被计算到截取长度内。 |
3 | boolean | No | FALSE |
是否按单词截取FALSE ,或是按字符截取TRUE 。 |
4 | boolean | No | FALSE |
当字符截取的长度刚好等于字符本身长度时,是否截取。
FALSE 也会截取。 TRUE 是不会截取。
注意如果设置为TRUE ,单词的边界会被忽略。
|
Example 5.21. truncate
<?php $smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.'); ?>
模板:
{$articleTitle} {$articleTitle|truncate} {$articleTitle|truncate:30} {$articleTitle|truncate:30:""} {$articleTitle|truncate:30:"---"} {$articleTitle|truncate:30:"":true} {$articleTitle|truncate:30:"...":true} {$articleTitle|truncate:30:'..':true: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... Two Sisters Re..ckout Counter.