这篇文章主要介绍了Smarty中常用变量操作符,实例汇总了常见的各种变量操作符,非常具有实用价值,需要的朋友可以参考下
本文汇总了Smarty中常用变量操作符,分享给大家供大家参考。具体如下:
php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用"|"应用变量操作符,多个参数用":"??指簟?/DIV>
capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成
]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符
实例如下:
复制代码 代码如下:
{* 标题大写 *}
index.php页面如下:
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('index.tpl');
index.tpl页面如下:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|capitalize}
OUTPUT输出如下:
复制代码 代码如下:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.
count_characters(计算变量里的字符数)
index.php如下:
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('index.tpl');
index.tpl页面如下:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|count_characters}
OUTPUT输出如下:
Cold Wave Linked to Temperatures.
cat(连接字符串)
将cat里的值连接到给定的变量后面
index.php如下:
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Psychics predict world didn't end');
$smarty->display('index.tpl');
index.tpl页面如下:
复制代码 代码如下:
{$articleTitle|cat:" yesterday."}
OUTPUT输出如下:
复制代码 代码如下:
Psychics predict world didn't end yesterday.
count_paragraphs(计算段数)
计算变量里的段落数量
index.php如下:
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.');
$smarty->display('index.tpl');
index.tpl模板页面如下:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|count_paragraphs}
OUTPUT输出如下:
复制代码 代码如下:
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2
count_sentences(计算句数)
计算变量里句子的数量
index.php如下:
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('index.tpl');
index.tpl模板如下:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|count_sentences}
OUTPUT输出如下:
复制代码 代码如下:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('index.tpl');
index.tpl模板如下:
复制代码 代码如下:
{$articleTitle}
{$articleTitle|count_words}
OUTPUT输出如下:
复制代码 代码如下:
Dealers Will Hear Car Talk at Noon.
7
复制代码 代码如下:
$smarty = new Smarty;
$smarty->assign('yesterday', strtotime('-1 day'));
$smarty->display('index.tpl');
index.tpl:
复制代码 代码如下:
{$smarty.now|date_format}
{$smarty.now|date_format:"%A, %B %e, %Y"}
{$smarty.now|date_format:"%H:%M:%S"}
{$yesterday|date_format}
{$yesterday|date_format:"%A, %B %e, %Y"}
{$yesterday|date_format:"%H:%M:%S"}
OUTPUT输出如下:
复制代码 代码如下: