smarty中 truncate replace 函数的使用

truncate

参数顺序类型必选参数默认值说明
1 integer No 80 截取的长度
2 string No ... 截取后替代显示的字符,该字符长度会被计算到截取长度内。
3 boolean No FALSE 是否按单词截取FALSE,或是按字符截取TRUE
4 boolean No FALSE 当字符截取的长度刚好等于字符本身长度时,是否截取。 FALSE也会截取。 TRUE是不会截取。 注意如果设置为TRUE,单词的边界会被忽略。 
<?php
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
?>

模板:

1 {$articleTitle}
2 {$articleTitle|truncate}
3 {$articleTitle|truncate:30}
4 {$articleTitle|truncate:30:""}
5 {$articleTitle|truncate:30:"---"}
6 {$articleTitle|truncate:30:"":true}
7 {$articleTitle|truncate:30:"...":true}
8 {$articleTitle|truncate:30:'..':true:true}

输出:
1  Two Sisters Reunite after Eighteen Years at Checkout Counter.
2  Two Sisters Reunite after Eighteen Years at Checkout Counter.
3  Two Sisters Reunite after...
4  Two Sisters Reunite after
5  Two Sisters Reunite after---
6  Two Sisters Reunite after Eigh
7  Two Sisters Reunite after E...
8  Two Sisters Re..ckout Counter.

参数顺序类型必选参数默认值说明
1 string Yes n/a 需要搜索并替换的字符
2 string Yes n/a 替换用的字符

 replace

$smarty->assign('articleTitle', "Child's Stool Great for Use in Garden.");

模板:

{$articleTitle}
{$articleTitle|replace:'Garden':'Vineyard'}
{$articleTitle|replace:' ':'   '}   

输出:

Child's Stool Great for Use in Garden.
Child's Stool Great for Use in Vineyard.
Child's   Stool   Great   for   Use   in   Garden.

联合使用
<{$nutritionList.disease_name|truncate:80|replace:",":"、"}>
posted @ 2016-11-29 17:44  歲夢灬沉浮  阅读(171)  评论(0)    收藏  举报