在smarty可以使用{$variable|cat:"要合併的字串"}來將變數及字串合併。(在PHP要這樣使用-$variable . "要合併的字串")
其語法:{$variable|函數名稱:參數…}
主要函數說明:
capitalize 英文字首大寫
{$variable|capitalize} 每個單字中若有非英文字母者不轉換
{$variable|capitalize:true}
cat 字串合併
{$variable|cat:"字串內容"}
count_characters算字串長度
{$variable|count_characters} 不計算空白及標點符號。
{$variable|count_characters:true}
count_paragraphs計算段落數
{$variable|count_paragraphs}
count_sentences 計算句數
{$variable|count_sentences}
count_words 計算單字數
{$variable|count_words}
default 設定變數預設值
{$variable|default:"字串內容"} 變數無內容時的預設值
date_format 轉換時間格式
{$variable| date_format:"時間格式字串"} 設定時間的格式 (時間參數請參照官網)
string_format 轉換數字格式
{$variable|string_format:"%.2f"} 傳回小數點兩位
{$variable|string_format:"%d"} 傳回整數
escape將字串轉換成編碼
{$variable|escape} 預設為參數"html"
{$variable|escape:"html"} 將 & " ' < > 等字元轉換為編碼
{$variable|escape:"htmlall"} 轉換所有的html
{$variable|escape:"url"} 轉換成url編碼
{$variable|escape:"quotes"} 轉換引號
{$variable|escape:"hex"} 轉換16進位(網址)
{$variable|escape:"hexentity"} 轉換16進位(字串)
upper轉成大寫
{$variable|upper}
lower 轉成小寫
{$variable|lower}
nl2br 將換行字元取代html的
{$variable|nl2br}
replace 取代字串
{$variable|replace:"被取代的字串":"取代成的字串"}
regex_replace 使用正規式取代字串
{$variable|replace:"正規式":"取代成的字串"}
truncate 裁切字串
{$variable|truncate} 預設長度為前80字元
{$variable|truncate:數字} 自訂裁切的長度,預設的字串結尾為"..."
{$variable|truncate:數字:"字串"} 自訂結尾的字串
{$variable|truncate:數字:"字串":true} 長度計算包含結尾字串的長度
spacify 在字串的每個字元間插入空白
{$variable|spacify} 在每個字元之間插入一個空白
{$variable|spacify:"字串內容"} 在每個字元之間插入自訂的字串
strip 除去多餘的空白
{$variable|strip} 除去多餘1個以上的空白
{$variable|strip:"字串內容"} 將空白換成自訂的字串
strip_tags 除去html的標籤
{$variable|strip_tags}
indent設定縮排
{$variable|indent} 預設的縮排為四格空白
{$variable|indent:數目} 縮排格空白數
{$variableindent:數目:"字串內容"} 自訂縮排的字串(非空白)
wordwrap 設定文字邊界
{$variable|wordwrap:長度} 設定每行文字的字數
{$variable|wordwrap:長度:"換行字串"} 設定換行結尾的字串
{$variable|wordwrap:長度:"換行字串":true} 長度計算不包含換行的字串
2010年9月22日
smarty用法
模板語言Smarty-變數修改器
修改器使用方式
{$變數名稱|函數名稱:參數…}
修改器的組合使用
{$變數名稱|函數1名稱:參數…|函數2名稱:參數…} 函數可以一直加下去
修改器列表如下:
判斷 array 是否為空
{if $array|@count == 0}EMPTY{/if}
{$number|string_format:”%.2f”} 傳回小數點兩位
capitalize 英文字首大寫
{$變數|capitalize} 每個單字中若有非英文字母者不轉換
{$變數|capitalize:true}
cat 字串合併
{$變數|cat:”字串內容”}
count_characters算字串長度
{$變數|count_characters} 不計算空白及標點符號。
{$變數|count_characters:true}
count_paragraphs計算段落數
{$變數|count_paragraphs}
count_sentences 計算句數
{$變數|count_sentences}
count_words 計算單字數
{$變數|count_words}
default 設定變數預設值
{$變數|default:”字串內容”} 變數無內容時的預設值
date_format 轉換時間格式
{$變數| date_format:”時間格式字串”} 設定時間的格式 (時間參數請參照官網)
string_format 轉換數字格式
{$number|string_format:”%.2f”} 傳回小數點兩位
{$number|string_format:”%d”} 傳回整數
escape將字串轉換成編碼
{$articleTitle|escape} 預設為參數”html”
{$articleTitle|escape:”html”} 將 & ” ‘ < > 等字元轉換為編碼
{$articleTitle|escape:”htmlall”} 轉換所有的html
{$articleTitle|escape:”url”} 轉換成url編碼
{$articleTitle|escape:”quotes”} 轉換引號
{$EmailAddress|escape:”hex”} 轉換16進位(網址)
{$EmailAddress|escape:”hexentity”} 轉換16進位(字串)
upper轉成大寫
{$變數|upper}
lower 轉成小寫
{$變數|lower}
nl2br 將換行字元取代html的
{$變數|nl2br}
replace 取代字串
{$變數|replace:”被取代的字串”:”取代成的字串”}
regex_replace 使用正規式取代字串
{$變數|replace:”正規式”:”取代成的字串”}
truncate 裁切字串
{$變數|truncate} 預設長度為前80字元
{$變數|truncate:數字} 自訂裁切的長度,預設的字串結尾為”…”
{$變數|truncate:數字:”字串”} 自訂結尾的字串
{$變數|truncate:數字:”字串”:true} 長度計算包含結尾字串的長度
spacify 在字串的每個字元間插入空白
{$變數|spacify} 在每個字元之間插入一個空白
{$變數|spacify:”字串內容”} 在每個字元之間插入自訂的字串
strip 除去多餘的空白
{$變數|strip} 除去多餘1個以上的空白
{$變數|strip:”字串內容”} 將空白換成自訂的字串
strip_tags 除去html的標籤
{$變數|strip_tags}
indent設定縮排
{$變數|indent} 預設的縮排為四格空白
{$變數|indent:數目} 縮排格空白數
{$變數|indent:數目:”字串內容”} 自訂縮排的字串(非空白)
wordwrap 設定文字邊界
{$變數|wordwrap:長度} 設定每行文字的字數
{$變數|wordwrap:長度:”換行字串”} 設定換行結尾的字串
{$變數|wordwrap:長度:”換行字串”:true} 長度計算不包含換行的字串
數字加上千分號
變數 | number_format:”"
tpl 內設定變數
<{assign var=total value=$info.total_amount+$info.amount_1 }>
陣列數量
{$陣列名稱|@count}
修改器使用方式
{$變數名稱|函數名稱:參數…}
修改器的組合使用
{$變數名稱|函數1名稱:參數…|函數2名稱:參數…} 函數可以一直加下去
修改器列表如下:
判斷 array 是否為空
{if $array|@count == 0}EMPTY{/if}
{$number|string_format:”%.2f”} 傳回小數點兩位
capitalize 英文字首大寫
{$變數|capitalize} 每個單字中若有非英文字母者不轉換
{$變數|capitalize:true}
cat 字串合併
{$變數|cat:”字串內容”}
count_characters算字串長度
{$變數|count_characters} 不計算空白及標點符號。
{$變數|count_characters:true}
count_paragraphs計算段落數
{$變數|count_paragraphs}
count_sentences 計算句數
{$變數|count_sentences}
count_words 計算單字數
{$變數|count_words}
default 設定變數預設值
{$變數|default:”字串內容”} 變數無內容時的預設值
date_format 轉換時間格式
{$變數| date_format:”時間格式字串”} 設定時間的格式 (時間參數請參照官網)
string_format 轉換數字格式
{$number|string_format:”%.2f”} 傳回小數點兩位
{$number|string_format:”%d”} 傳回整數
escape將字串轉換成編碼
{$articleTitle|escape} 預設為參數”html”
{$articleTitle|escape:”html”} 將 & ” ‘ < > 等字元轉換為編碼
{$articleTitle|escape:”htmlall”} 轉換所有的html
{$articleTitle|escape:”url”} 轉換成url編碼
{$articleTitle|escape:”quotes”} 轉換引號
{$EmailAddress|escape:”hex”} 轉換16進位(網址)
{$EmailAddress|escape:”hexentity”} 轉換16進位(字串)
upper轉成大寫
{$變數|upper}
lower 轉成小寫
{$變數|lower}
nl2br 將換行字元取代html的
{$變數|nl2br}
replace 取代字串
{$變數|replace:”被取代的字串”:”取代成的字串”}
regex_replace 使用正規式取代字串
{$變數|replace:”正規式”:”取代成的字串”}
truncate 裁切字串
{$變數|truncate} 預設長度為前80字元
{$變數|truncate:數字} 自訂裁切的長度,預設的字串結尾為”…”
{$變數|truncate:數字:”字串”} 自訂結尾的字串
{$變數|truncate:數字:”字串”:true} 長度計算包含結尾字串的長度
spacify 在字串的每個字元間插入空白
{$變數|spacify} 在每個字元之間插入一個空白
{$變數|spacify:”字串內容”} 在每個字元之間插入自訂的字串
strip 除去多餘的空白
{$變數|strip} 除去多餘1個以上的空白
{$變數|strip:”字串內容”} 將空白換成自訂的字串
strip_tags 除去html的標籤
{$變數|strip_tags}
indent設定縮排
{$變數|indent} 預設的縮排為四格空白
{$變數|indent:數目} 縮排格空白數
{$變數|indent:數目:”字串內容”} 自訂縮排的字串(非空白)
wordwrap 設定文字邊界
{$變數|wordwrap:長度} 設定每行文字的字數
{$變數|wordwrap:長度:”換行字串”} 設定換行結尾的字串
{$變數|wordwrap:長度:”換行字串”:true} 長度計算不包含換行的字串
數字加上千分號
變數 | number_format:”"
tpl 內設定變數
<{assign var=total value=$info.total_amount+$info.amount_1 }>
陣列數量
{$陣列名稱|@count}
關鍵字
smarty
2008年11月25日
smarty truncate 截取字符串長度
smarty truncate 截取
從字符串開始處截取某長度的字符.默認是80個。
你也可以指定第二個參數作為追加在截取字符串後面的文本字串.該追加字串被計算在截取長度中。
默認情況下,smarty會截取到一個詞的末尾。
如果你想要精確的截取多少個字符,把第三個參數改為"true" 。
例 截取:
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
輸出結果:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
從字符串開始處截取某長度的字符.默認是80個。
你也可以指定第二個參數作為追加在截取字符串後面的文本字串.該追加字串被計算在截取長度中。
默認情況下,smarty會截取到一個詞的末尾。
如果你想要精確的截取多少個字符,把第三個參數改為"true" 。
例 截取:
index.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Sisters Reunite after Eighteen Years at Checkout Counter.');
$smarty->display('index.tpl');
index.tpl:
{$articleTitle}
{$articleTitle|truncate}
{$articleTitle|truncate:30}
{$articleTitle|truncate:30:""}
{$articleTitle|truncate:30:"---"}
{$articleTitle|truncate:30:"":true}
{$articleTitle|truncate:30:"...":true}
輸出結果:
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after Eighteen Years at Checkout Counter.
Two Sisters Reunite after...
Two Sisters Reunite after
Two Sisters Reunite after---
Two Sisters Reunite after Eigh
Two Sisters Reunite after E...
關鍵字
smarty
訂閱:
文章 (Atom)