Smarty變量調節器的使用

$str = 'how Are you';
$smarty->assign('str', $str);

$title = "\nbbbbbb。bbbb\n1111111\nbbbbbbbb\n222.\t22.22";
$smarty->assign('title',$title);

$name = "who are \n you?";
$smarty->assign('name',$name);

$address = "<center>I'm center</center><sm>ddddddd</sm>";
$smarty->assign('address',$address);

$leg = "1a2 admin b3c4d5e6g7h8i9j";
$smarty->assign('leg',$leg);

$file = "hi who are you where are from";
$smarty->assign('file',$file);

$num = 123.4567;
$smarty->assign('num',$num);

$content = "hit me <a href='#'>this is a test</a><?c=admin&a=input>[email protected]";
$smarty->assign('content',$content);

$smarty->display('index.html');		//顯示index.html

html

<{$str}><br>
<{$str|capitalize}><{*首字母大寫*}><br>
<{$str|upper}><{*全部大寫*}><br>
<{$str|lower}><{*全部小寫*}><br>
<{$str|lower|upper}><{*全部大寫,調節從左至右*}><br>
<{$str|count_characters}><{*統計字符的個數*}><br>
<{$str|count_characters:true}><{*統計字符的個數,包括空格*}><br>
<{$str|cat:"??????"}><{*連接到變量的值後面*}><br><br>

<{$title|count_paragraphs}><{*統計變量中段落的個數,即相鄰\n或\r的會算爲一個*}><br>
<{$title|count_sentences}><{*統計變量中句子的個數*}><br>
<{$title|count_words}><{*統計變量中單詞的個數*}><br><br>

<{$smarty.now|date_format:"%Y-%m-%d %p %H:%M:%S week:%W"}><{*當前時間2012-04-25 AM 08:20:17 week:17*}><br><>br

<{$user|default:"null"}><{*如果變量爲空或者不存在時,輸出默認的值,即default的值*}><br><br>

<{$name}><br>
<{$name|indent}><br>
<{$name|indent:10}><br>
<{$name|indent:1:"\t"}><br><br>

<{$name|regex_replace:"/[\r\n\t]/":"string"}><{*正則替換*}><br><br>

<{$name|replace:"who":"where"}><{*替換*}><br><br>

<{$name|spacify:"!"}><{*在字符之間插入字符*}><br><br>

<{$name|strip}><{*用空格或指定字符替換所有重複空格、換行、製表符*}><br>
<{$name|strip:" "}><br><br>

<{$address}><br>
<{$address|strip_tags}><{*去掉<>標籤及中間的內容*}><br><br>

<{$leg|truncate}><{*字符串截取,默認80個字符*}><br>
<{$leg|truncate:10}><{*字符串截取,前10個,包括...三個字符*}><br>
<{$leg|truncate:10:""}><{*字符串截取,前10個字符,不用...代替後面的內容*}><br>
<{$leg|truncate:10:"---"}><{*字符串截取,前10個字符,以單詞結束,用---代替後面的內容*}><br>
<{$leg|truncate:10:"...":true}><{*字符串截取,前10個字符,精確個數,用...代替後面的內容*}><br>
<{$leg|truncate:10:"":true}><{*字符串截取,前10個字符,不用...代替後面的內容*}><br><br>

<{$file}><br>
<{$file|wordwrap:10}><{*10字符爲一行*}><br>
<{$file|wordwrap:10:"<br>\n"}><{*10字符爲一行*}><br>
<{$file|wordwrap:10:"\n":true}><{*10字符爲一行*}><br><br>


<{$num}><br>
<{$num|string_format:"%.2f"}><{*取兩位小數,四捨五入*}><br>
<{$num|string_format:"%d"}><{*取整*}><br><br>


<{$content}><br>
<{$content|escape}><br>
<{$content|escape:"html"}><br>
<{$content|escape:"htmlall"}><br>
<{$content|escape:"url"}><br>
<{$content|escape:"quotes"}><br>
<{$content|escape:"hex"}><br>
<{$content|escape:"hexentity"}><br>
<{$content|escape:"javascript"}><br>

變量調節器,可以組合使用,來達到顯示的效果,用"|"分隔,從左到右執行

結果:



發佈了47 篇原創文章 · 獲贊 25 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章