Smarty中常用变量操作符汇总

前端技术 2023/09/08 PHP

本文汇总了Smarty中常用变量操作符,分享给大家供大家参考。具体如下:

php模板引擎smarty的变量操作符可用于操作变量,自定义函数和字符。
语法中使用\"|\"应用变量操作符,多个参数用\":\"??指簟?/DIV>

capitalize[首字母大写]
count_characters[计算字符数]
cat[连接字符串]
count_paragraphs[计算段落数]
count_sentences[计算句数]
count_words[计算词数]
date_format[时间格式]
default[默认]
escape[转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成<br />]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束]
组合使用多个操作符

实例如下:

复制代码 代码如下:
{* 标题大写 *}
<h2>{$title|upper}</h2>
{* 取其前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

本文地址:https://www.stayed.cn/item/21766

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。