PHP教程
+ -

smarty模板中truncate的使用

2022-01-02

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

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...

0 篇笔记 写笔记

smarty模板中truncate的使用
truncate:截取1.从字符串开始处截取某长度的字符,默认是80个,你也可以指定第二个参数作为追加在截取字符串后面的文本字串.该追加字串被计算在截取长度中。默认情况下,smarty会截取到一个词的末尾。如果你想要精确的截取多少个字符,把第三个参数改为”true”;2.例:截取index.php......
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!