PHP模板引擎Smarty內建變數調解器用法詳解_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了PHP模板引擎Smarty內建變數調解器用法。分享給大家供大家參考,具體如下:

Smarty 中的變數調解器相當於函數,其調用方式為:通過 "|" 後面直接跟調解器函數名,如果有參數,得加在 ":" 後面,多個參數的話,累加即可。

下面為您介紹 Smarty 中內建的變數調解器:

1、capitalize

將變數裡的所有單詞首字大寫。參數值 boolean 型決定帶數位單詞,首字是否大寫。預設不大寫

index.php

$tpl->assign('str', 'hello world wor2ld!!!');$tpl->display('index.html');

index.html(模板檔案)

<{$str|capitalize}><{$str|capitalize:true}>

結果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!

2、count_characters

計算變數裡的字元數,該調解器預設不計算空格(空格、定位字元、斷行符號…)只計算字元的個數,並且能很好的支援中文字元計算;如果添加參數 true ,則計算空格。

index.html

<{$str|count_characters}> // 不計算空格<{$str|count_characters:true}> // 計算空格

結果為:13、14

3、cat

連接字串,將cat裡的值串連到給定的變數後面。

<{$str|cat:' Happy new year.'}>

結果為:hello world!!! Happy new year.

4、count_paragraphs

計算段數,計算變數裡的段落數量,完美支援中文段落。

index.php

$str = <<assign('str', $str);$tpl->display('index.html');

index.html

<{$str|count_paragraphs}>

結果為:3

5、count_sentences

計算句數,計算變數裡句子的數量。註:只支援英文語句,不支援中文。

index.php

$str = <<assign('str', $str);

index.html

<{$str|count_sentences}>

結果為:2

6、count_words

計算詞數,計算變數裡的詞數。

index.php

$str = <<assign('str', $str);

index.html

<{$str|count_words}>

結果為:12

7、date_format

日期格式化,具體參數很多,這裡只舉例中國式日期格式

index.php

$tpl->assign('date', time()); // 傳遞時間戳記

index.html

<{$date|date_format:'%Y-%m-%d %H:%M:%S'}>

結果為:2012-01-26 14:37:22

8、default

預設,為空白變數設定一個預設值,當變數為空白或者未分配的時候,將由給定的預設值替代輸出。

index.php

$tpl->assign('str', ''); // 賦值給空

index.html

<{$str|default:'預設輸出...'}>、<{$string|default:'沒有定義,預設輸出...'}>

結果為:預設輸出...、沒有定義,預設輸出...

9、escape

轉碼,用於 html 轉碼,url 轉碼,在沒有轉碼的變數上轉換單引號,十六進位轉碼,十六進位美化,或者 javascript 轉碼,預設是html轉碼

index.php

$html = <<Googlehtml;$js = <<  for (var i=0; i<100; i++) {    window.alert(i);  }js;$tpl->assign('html', $html); // html$tpl->assign('url', 'http://www.google.com.hk'); // url$tpl->assign('js', $js); // javascript

index.html

HTML 轉碼:<{$html|escape:"html"}>URL 轉碼:<{$url|escape:"url"}>JS 轉碼:<{$js|escape:"javascript"}>

結果為:

HTML 轉碼:GoogleURL 轉碼:http%3A%2F%2Fwww.google.com.hkJS 轉碼:

10、indent

縮排,每行縮排字串,第一個參數指定縮排多少個字串,預設是四個字元;第二個參數,指定縮排用什麼字元代替。

11、lower

小寫,將變數字串小寫。

使用方法:<{$str|lower}>

12、upper

大寫,將變數改為大寫。

使用方法:<{$str|upper}>

13、nl2br

分行符號替換成

所有的分行符號將被替換成 ,同php的nl2br()函數一樣。

14、regex_replace

正則替換,尋找和替換Regex,和 preg_replace() 的文法一樣。

index.php

$tpl->assign('str', 'http://www.google.com');

index.html

<{$str|regex_replace:'/go{2}gle/':'baidu'}>

結果為:http://www.baidu.com

15、replace

替換,簡單的搜尋和替換字串。

16、spacify

插空,插空(不知道這個詞是什麼意思,顧名思義了^^)是一種在字串的每個字元之間插入空格或者其他的字元(串)。

index.php

$tpl->assign('str', 'hello world!!!');

index.html

<{$str|spacify:"^^"}>

結果為:h^^e^^l^^l^^o^^ ^^w^^o^^r^^l^^d^^!^^!^^!

17、string_format

字串格式化,是一種格式化浮點數的方法,例如:十進位數.使用 sprintf 文法格式化。

index.php

$tpl->assign('num', 23.5787446);

index.html

<{$num|string_format:"%.2f"}><{$num|string_format:"%d"}>

結果為:23.58、23

18、strip

替換所有重複的空格、換行、tab 為單個

index.php

$tpl->assign('str', "Grandmother of\neight makes\t  hole in one.");

index.html

<{$str|strip:" "}>

結果為:Grandmother of eight makes hole in one.

原始碼:

Grandmother of eight makes hole in one.

19、strip_tags

去除在<和>之間的所有標籤,包括<和>。

index.php

$tpl->assign('str', "Google");

index.html

<{$str|strip_tags}>

結果為:Google(原始碼也是 Google,去掉了標籤和標籤)

20、truncate

截取,截取字串開始的一段.預設是80個,你可以指定第二個參數作為在截取的那段字串後加上什麼字元,預設情況下,smarty會截取到一個詞的末尾,如果你想要精確的截取多少個字元,把第三個參數改為"true" 。

index.php

複製代碼 代碼如下:
$tpl->assign('str', '從前有座山,山上有座廟。廟裡有一個老和尚和一個小和尚...');

index.html

<{$str|truncate:10:'...':true}>

結果為:從前有座山,山...

更多關於PHP相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於smarty模板的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.