龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

Smarty中常用变量操作符汇总(5)

时间:2014-10-28 15:39来源:网络整理 作者:网络 点击:
分享到:
string_format(字符串格式化) Parameter Position Type Required Default Description 1 string Yes n/a This is what format to use. (sprintf) 使用的格式化方式 是一种格式化浮点数的方法

string_format(字符串格式化)
Parameter Position Type Required Default Description
1 string Yes n/a This is what format to use. (sprintf)
使用的格式化方式
是一种格式化浮点数的方法.例如十进制数.使用sprintf语法格式化
index.php如下:

复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('number', 23.5787446);
$smarty->display('index.tpl');

index.tpl模板:
复制代码 代码如下:
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}

OUTPUT输出:
复制代码 代码如下:
23.5787446
23.58
24

strip(去除(多余空格)
替换所有重复的空格,换行和tab为单个.
index.php如下:

复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Grandmother ofneight makest hole in one.");
$smarty->display('index.tpl');

index.tpl模板:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|strip}
{$articleTitle|strip:" "}

OUTPUT输出:
复制代码 代码如下:
Grandmother of
eight makes hole in one.
Grandmother of eight makes hole in one.
Grandmother of eight makes hole in one.

strip_tags(去除html标签)
去除在<和>之间的所有标签,包括<和>.
index.php如下:

复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.");
$smarty->display('index.tpl');

index.tpl模板:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|strip_tags}

OUTPUT输出:
复制代码 代码如下:
Blind Woman Gets <font face="helvetica">New Kidney</font> from Dad she Hasn't Seen in <b>years</b>.
Blind Woman Gets New Kidney from Dad she Hasn't Seen in years.

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).
检查是否截取到词的边界
截取字符串开始的一段.默认是80个.
你可以指定第二个参数作为在截取的那段字符串后加上什么字符.
默认情况下,smarty会截取到一个词的末尾,
如果你想要精确的截取多少个字符,把第三个参数改为"true"
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}

OUTPUT输出:
复制代码 代码如下:
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...

精彩图集

赞助商链接