php smarty模版引擎中變量操作符及使用方法
更新時(shí)間:2009年12月11日 16:53:17 作者:
php smarty模版引擎中常用的20個(gè)變量操作符及使用方法
smarty常用的20個(gè)變量操作符 * 使用語(yǔ)法:{變量名|操作符:}
* capitalize ---首字母大寫
* count_characters ---計(jì)算字符數(shù)
* cat ---連接字符串
* count_paragraphs ---計(jì)算段落數(shù)
* count_sentences ---計(jì)算句數(shù)
* count_words ---計(jì)算詞數(shù)
* date_format ---時(shí)間格式
* default ---默認(rèn)
* escape ---轉(zhuǎn)碼
* indent ---縮進(jìn)
* lower ---小寫
* nl2br ---換行符替換為
* regex_replace ---正則替換
* replace ---替換
* spacify ---插空
* string_format ---字符串格式化
* strip ---去除多余空格
* strip_tags ---去除html標(biāo)簽
* truncate ---截取
* upper ---大寫
* wordwrap --約束行寬
使用方法:
index.php
include("smarty_inc.php");
$name = "My name is MaJi,age 22,sex boy.<a href=>aaaaaa</a>.";
$smarty->assign("title", $name);
$smarty->assign("row", $row);
$smarty->assign("d",strtotime("-0"));
$smarty->assign("nubmer", 342345.736524);
$smarty->display("index.html");
?>
index.html
原始數(shù)據(jù):{$title}
使用capitalize變量操作符后:{$title|capitalize}
使用count_characters變量操作符后:{$title|count_characters}
使用cat變量操作符后:{$title|cat:"wwww.baidu.com"}
使用count_paragraphs變量操作符后:{$title|count_paragraphs}
使用count_sentences變量函數(shù)操作符后:{$title|count_sentences}
使用count_words變量函數(shù)操作后:{$title|count_words}
原始時(shí)間數(shù)據(jù):{$d}
使用date_format變量函數(shù)操作:{$d|date_format:"%Y-%m-%d"}
使用smarty.now調(diào)用時(shí)間:{$smarty.now|date_format:"%Y-%m-%d"}
使用default變量函數(shù)操作:{$title1|default:"沒(méi)有這個(gè)變量"}
使用escape變量函數(shù)操作:{$title|escape:"html"}
使用indent變量函數(shù)操作:{$title|indent:2:" "}
使用lower變量函數(shù)操作: {$title|lower}
使用upper變量函數(shù)操作:{$title|upper}
使用replace變量函數(shù)操作:{$title|replace:"is":"@@"}
使用spacify變量函數(shù)操作:{$title|spacify:"_"}
使用string_format變量函數(shù)操作:{$nubmer|string_format:"%.2f"}
使用strip變量函數(shù)操作:{$title|strip:"_"}
使用strip_tags變量函數(shù)操作:{$title|strip_tags}
使用truncate變量函數(shù)操作:{$title|truncate:30:"..."}
使用wordwrap變量函數(shù)操作:{$title|wordwrap:10:"<br>"}
* capitalize ---首字母大寫
* count_characters ---計(jì)算字符數(shù)
* cat ---連接字符串
* count_paragraphs ---計(jì)算段落數(shù)
* count_sentences ---計(jì)算句數(shù)
* count_words ---計(jì)算詞數(shù)
* date_format ---時(shí)間格式
* default ---默認(rèn)
* escape ---轉(zhuǎn)碼
* indent ---縮進(jìn)
* lower ---小寫
* nl2br ---換行符替換為
* regex_replace ---正則替換
* replace ---替換
* spacify ---插空
* string_format ---字符串格式化
* strip ---去除多余空格
* strip_tags ---去除html標(biāo)簽
* truncate ---截取
* upper ---大寫
* wordwrap --約束行寬
使用方法:
index.php
復(fù)制代碼 代碼如下:
include("smarty_inc.php");
$name = "My name is MaJi,age 22,sex boy.<a href=>aaaaaa</a>.";
$smarty->assign("title", $name);
$smarty->assign("row", $row);
$smarty->assign("d",strtotime("-0"));
$smarty->assign("nubmer", 342345.736524);
$smarty->display("index.html");
?>
index.html
原始數(shù)據(jù):{$title}
使用capitalize變量操作符后:{$title|capitalize}
使用count_characters變量操作符后:{$title|count_characters}
使用cat變量操作符后:{$title|cat:"wwww.baidu.com"}
使用count_paragraphs變量操作符后:{$title|count_paragraphs}
使用count_sentences變量函數(shù)操作符后:{$title|count_sentences}
使用count_words變量函數(shù)操作后:{$title|count_words}
原始時(shí)間數(shù)據(jù):{$d}
使用date_format變量函數(shù)操作:{$d|date_format:"%Y-%m-%d"}
使用smarty.now調(diào)用時(shí)間:{$smarty.now|date_format:"%Y-%m-%d"}
使用default變量函數(shù)操作:{$title1|default:"沒(méi)有這個(gè)變量"}
使用escape變量函數(shù)操作:{$title|escape:"html"}
使用indent變量函數(shù)操作:{$title|indent:2:" "}
使用lower變量函數(shù)操作: {$title|lower}
使用upper變量函數(shù)操作:{$title|upper}
使用replace變量函數(shù)操作:{$title|replace:"is":"@@"}
使用spacify變量函數(shù)操作:{$title|spacify:"_"}
使用string_format變量函數(shù)操作:{$nubmer|string_format:"%.2f"}
使用strip變量函數(shù)操作:{$title|strip:"_"}
使用strip_tags變量函數(shù)操作:{$title|strip_tags}
使用truncate變量函數(shù)操作:{$title|truncate:30:"..."}
使用wordwrap變量函數(shù)操作:{$title|wordwrap:10:"<br>"}
相關(guān)文章
Pain 全世界最小最簡(jiǎn)單的PHP模板引擎 (普通版)
PHP全世界最小最簡(jiǎn)單的模板引擎------Pain (普通版),不同于smarty,所有的變量以{@ @}括起來(lái),有助于大家對(duì)模板引擎的了解。2011-10-10ThinkPHP關(guān)聯(lián)模型操作實(shí)例分析
ThinkPHP關(guān)聯(lián)模型操作實(shí)例分析,需要的朋友可以參考下2012-09-09