Smarty技巧篇
价格格式化:<{$test|number_format:2:".":","}>
- capitalize [首字符大寫]
{$articleTitle}
{$articleTitle|capitalize} - count_characters [計算字數]
{$articleTitle}
{$articleTitle|count_characters}
{$articleTitle|count_characters:true}
第一個變數決定是否計算空格字符 - cat [連接字串]
{$articleTitle|cat:" yesterday."} - count_paragraphs [計算段落]
{$articleTitle|cat:" yesterday."} - count_sentences [計算句數]
{$articleTitle}
{$articleTitle|count_sentences} - count_words [計算字數]
{$articleTitle}
{$articleTitle|count_words} - date_format [格式化日期]
{$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"} - default [默認值]
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"} - escape [編碼] 用於html轉碼,url轉碼,在沒有轉碼的變數上轉換單引號,十六進制轉碼,十六進制美化,或者javascript轉碼。需設是html轉碼。 {$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a> - indent [縮進]在每行縮進字符串,默認是4個字符。
作為可選參數,你可以指定縮進字符數。
作為第二個可選參數,你可以指定縮進用什麼字符代替。 {$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"\t"} - lower [小寫]
{$articleTitle}
{$articleTitle|lower} - nl2br [換行符替換成 <br />]
- regex_replace [正則替換]
{$articleTitle}
{$articleTitle|regex_replace:"/[\r\t\n]/":" "} - replace [替換]
{$articleTitle}
{$articleTitle|replace:"Garden":"Vineyard"}
{$articleTitle|replace:" ":" "} - spacify [插空] 在字串的每個字之間插入空格或者其他的字串.
{$articleTitle}
{$articleTitle|spacify}
{$articleTitle|spacify:"^^"} - number_format [數字格式化] 第一個變數用來指定小數點的長度,會自動四捨五入
{$number}
{$number|number_format:1}
{$number|number_format:2} - string_format [字符串格式化]
{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"} - strip [去除(多餘空格)]
- strip_tags [去除html標籤]
- truncate [截取]
- upper [大寫]
{$articleTitle}
{$articleTitle|upper} - wordwrap [行寬約束]
可以指定段落的寬度(也就是多少個字一行,超過這個字數換行).預設 80.
第二個參數可選,可以指定在約束點使用什麼字串 (預設是換行符\n).
預設情況下smarty將截取到詞尾,如果想精確到設定長度的字符,請將第三個參數設為ture {$articleTitle}
{$articleTitle|wordwrap:30}
{$articleTitle|wordwrap:20}
{$articleTitle|wordwrap:30:"<br>\n"}
{$articleTitle|wordwrap:30:"\n":true}