truncate
truncate 用于将变量截断为指定的字符长度,默认为 80。作为可选的第二个参数,您可以指定一个文本字符串,在变量被截断时显示在末尾。字符串中的字符将包括在原始截断长度内。默认情况下,truncate 将尝试在单词边界处截断。如果要在精确的字符长度处截断,请传递可选的第三个参数为 TRUE。
基本用法
{$myVar|truncate:40:"..."}
参数
| 参数位置 | 类型 | 必需 | 默认值 | 描述 |
|---|---|---|---|---|
| 1 | integer | 否 | 80 | 指定要截断的字符数。 |
| 2 | string | 否 | ... | 替换截断文本的文本字符串。其长度包括在截断长度设置中。 |
| 3 | boolean | 否 | FALSE | 确定是否在单词边界处截断(FALSE),还是在精确字符处截断(TRUE)。 |
| 4 | boolean | 否 | FALSE | 确定截断是否发生在字符串末尾(FALSE),还是在字符串中部(TRUE)。请注意,如果此设置为 TRUE,则忽略单词边界。 |
示例
<?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.