Home  >  Article  >  php教程  >  Smarty中常用变量操作符汇总

Smarty中常用变量操作符汇总

WBOY
WBOYOriginal
2016-06-06 20:18:501259browse

这篇文章主要介绍了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[行宽约束]
组合使用多个操作符

实例如下:

复制代码 代码如下:

{* 标题大写 *}

{$title|upper}


{* 取其前40个字符 *}
Topic: {$topic|truncate:40:"..."}
{* 格式化文字串 *}
{"now"|date_format:"%Y/%m/%d"}
{* 在自定义函数里应用调节器 *}
{mailto|upper address="main@cn-web.com"}
capitalize(首字母大写)

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


count_words(计算词数)
计算变量里的词数
index.php如下:

复制代码 代码如下:

$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


date_format(日期格式)
Parameter Position
参数位置 Type Required Default Description
1 string No %b %e, %Y This is the format for the outputted date.
输出字串的格式
2 string No n/a This is the default date if the input is empty.
输入为空时的默认设置
在给定的函数serftime();里格式日期和时间.
Unix或者mysql等的时间戳(parsable by strtotime)都可以传递到smarty.
设计者可以使用date_format完全控制日期格式.
如果传给date_format的数据是空的,将使用第二个参数作为时间格式
index.php如下:

复制代码 代码如下:

$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输出如下:

复制代码 代码如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn