本文實例講述了PHP模板引擎Smarty內建變數調解器用法。分享給大家供大家參考,具體如下:
Smarty 中的變數調解器相當於函數,其呼叫方式為:透過"|" 後面直接跟調解函數名,如果有參數,得加在":" 後面,多個參數的話,累加即可。
下面為您介紹 Smarty 中內建的變數調解器:
1、capitalize
將變數裡的所有單字首字大寫。參數值 boolean 型決定帶數字的單字,首字是否大寫。預設不大寫
index.php
1 2 | $tpl ->assign('str', 'hello world wor2ld!!!');
$tpl ->display('index.html');
|
登入後複製
index.html(範本檔案)
1 2 | <{ $str |capitalize}>
<{ $str |capitalize:true}>
|
登入後複製
結果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!
2、count_characters
,此調解器預設不計算空格(空格、製表符、回車…)只計算字元的個數,並且能很好的支援中文字元計算;如果新增參數true ,則計算空格。
index.html
1 2 | <{ $str |count_characters}>
<{ $str |count_characters:true}>
|
登入後複製
結果為:13、14
3、cat
連接字串,將cat裡的值連接到給定的變數後面。
1 | <{ $str |cat:' Happy new year.'}>
|
登入後複製
結果為:hello world!!! Happy new year.
4、count_paragraphs
計算段數,計算變數裡的段落數量,完美支持中文。
index.php
1 2 | $str = <<assign('str', $str );
$tpl ->display('index.html');
|
登入後複製
index.html
1 | <{ $str |count_paragraphs}>
|
登入後複製
ten,註:只支援英文語句,不支援中文。
index.php
1 | $str = <<assign('str', $str );
|
登入後複製
登入後複製
index.html
字結果數為:23553字數的數字計算、術語數。
index.php
1 | $str = <<assign('str', $str );
|
登入後複製
登入後複製
index.html
index.php
1 | $tpl ->assign(' date ', time());
|
登入後複製
index.html
1 | <{ $date |date_format:'%Y-%m-%d %H:%M:%S'}>
|
登入後複製
結果為:2012-01-26 14:37:22
8、default
默認,為空變量設置一個默認值,當變數為空或未分配的時候,將由給定的預設值替代輸出。
index.php
1 | $tpl ->assign('str', '');
|
登入後複製
index.html
1 | <{ $str | default :'默认输出...'}>、<{ $string | default :'没有定义,默认输出...'}>
|
登入後複製
.於html 轉碼,url 轉碼,在沒有轉碼的變數上轉換單引號,十六進位轉碼,十六進位美化,或javascript 轉碼,預設是html轉碼
index.php
1 2 3 4 5 6 7 8 9 10 | $html = <<Google
html;
$js = <<
for ( var i=0; i<100; i++) {
window.alert(i);
}
js;
$tpl ->assign('html', $html );
$tpl ->assign('url', 'http:
$tpl ->assign('js', $js );
|
登入後複製
index.html
1 2 3 | HTML 转码:<{ $html |escape: "html" }>
URL 转码:<{ $url |escape: "url" }>
JS 转码:<{ $js |escape: "javascript" }>
|
登入後複製
結果為:
1 2 3 | HTML 转码:Google
URL 转码:http%3A%2F%2Fwww.google.com.hk
JS 转码:
|
登入後複製
進多少個字符串,預設是四個字元;第二個參數,指定縮排用什麼字元取代。
11、lower
小寫,將變數字串小寫。
使用方法:<{$str|lower}>
12、upper
大寫,將變數改為大寫。
使用方法:<{$str|upper}>
13、nl2br
換行符替換成
所有的換行符將被替換成 ,同php的nl2br()函數一樣。
14、regex_replace
正規替換,尋找和替換正規表示式,和 preg_replace() 的語法一樣。
index.php
1 | $tpl ->assign('str', 'http:
|
登入後複製
index.html
1 | <{ $str |regex_replace:'/go{2}gle/':'baidu'}>
|
登入後複製
。
16、spacify
插空,插空(不知道這個字是什麼意思,顧名思義了^^)是一種在字串的每個字元之間插入空格或其他的字元(字串)。
index.php
1 | $tpl ->assign('str', 'hello world!!!');
|
登入後複製
index.html
^^
^^^^ ^l^^d^^!^^!^^!
17、string_format
字串格式化,是一種格式化浮點數的方法,例如:十進制數.使用sprintf 語法格式化。
index.php
1 | $tpl ->assign('num', 23.5787446);
|
登入後複製
index.html
1 2 | <{ $num |string_format: "%.2f" }>
<{ $num |string_format: "%d" }>
|
登入後複製
、23.58、23383.單一
index. php
1 | $tpl ->assign('str', "Grandmother of\neight makes\t hole in one." );
|
登入後複製
index.html
結果為:Grandmother of eightkes makes
hole in one.
19、strip_tags
去除在<和>之間的所有標籤,包括<和>。
index.php
1 | $tpl ->assign('str', "Google" );
|
登入後複製
index.html
.
截取,截取字符串开始的一段.默认是80个,你可以指定第二个参数作为在截取的那段字符串后加上什么字符,默认情况下,smarty会截取到一个词的末尾,如果你想要精确的截取多少个字符,把第三个参数改为"true" 。
index.php
1 | $tpl ->assign('str', '从前有座山,山上有座庙。庙里有一个老和尚和一个小和尚...');
|
登入後複製
index.html
1 | <{ $str |truncate:10:'...':true}>
|
登入後複製
结果为:从前有座山,山...
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。
更多PHP模板引擎Smarty内置变量调解器用法详解相关文章请关注PHP中文网!