首頁  >  文章  >  後端開發  >  PHP模板引擎Smarty內建變數調解器的用法及實例詳解

PHP模板引擎Smarty內建變數調解器的用法及實例詳解

墨辰丷
墨辰丷原創
2018-06-04 09:27:151140瀏覽

這篇文章主要介紹了PHP模板引擎Smarty內建變數調解器用法,結合實例形式詳細分析了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:&#39; Happy new year.&#39;}>

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

4、count_paragraphs

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

index.php

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

#index.html

<{$str|count_paragraphs}>

結果為:3

5、count_sentences

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

index.php

$str = <<assign(&#39;str&#39;, $str);

#index.html

<{$str|count_sentences}>

結果為:2

6、count_words

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

index.php

$str = <<assign(&#39;str&#39;, $str);

#index.html

<{$str|count_words}>

結果為:12

7、date_format

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

index.php

$tpl->assign(&#39;date&#39;, time()); // 传递时间戳

index.html

#
<{$date|date_format:&#39;%Y-%m-%d %H:%M:%S&#39;}>

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

8、default

默認,為空變數設定一個預設值,當變數為空或未指派的時候,將由給定的預設值替代輸出。

index.php

$tpl->assign(&#39;str&#39;, &#39;&#39;); // 赋值给空

#index.html

<{$str|default:&#39;默认输出...&#39;}>、<{$string|default:&#39;没有定义,默认输出...&#39;}>

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

9、escape

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

index.php

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

index.html

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

結果為:

HTML 转码:Google
URL 转码:http%3A%2F%2Fwww.google.com.hk
JS 转码:

10、indent

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

11、lower

小寫,將變數字串小寫。

使用方法:750a9f85df50488af5b1b9f9539cd4b5

#12、upper

大寫,變數改為大寫。

使用方法:98cba6331cf99290e107dcab961d573a

#13、nl2br

換行符號替換成

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

14、regex_replace

正規替換,尋找和替換正規表示式,和 preg_replace() 的語法一樣。

index.php

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

#index.html

<{$str|regex_replace:&#39;/go{2}gle/&#39;:&#39;baidu&#39;}>

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

15、replace

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

16、spacify

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

index.php

$tpl->assign(&#39;str&#39;, &#39;hello world!!!&#39;);

#index.html

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

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

#17、string_format

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

index.php

$tpl->assign(&#39;num&#39;, 23.5787446);

#index.html

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

結果為:23.58、23

18、strip

取代所有重複的空格、換行、tab 為單一

index.php

$tpl->assign(&#39;str&#39;, "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

去除在67bffed8d39c986beaddeed3e8cd5568之间的所有标签,包括67bffed8d39c986beaddeed3e8cd5568。

index.php

$tpl->assign(&#39;str&#39;, "Google");

index.html

<{$str|strip_tags}>

结果为:Google(源代码也是 Google,去掉了标签和标签)

20、truncate

截取,截取字符串开始的一段.默认是80个,你可以指定第二个参数作为在截取的那段字符串后加上什么字符,默认情况下,smarty会截取到一个词的末尾,如果你想要精确的截取多少个字符,把第三个参数改为"true" 。

index.php

复制代码 代码如下:

$tpl->assign('str', '从前有座山,山上有座庙。庙里有一个老和尚和一个小和尚...');

index.html

<{$str|truncate:10:&#39;...&#39;:true}>

结果为:从前有座山,山...

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP中Socket连接及读写数据超时问题分析

PHP实现的DES加密解密封装类

php中htmlentities()函数的定义及用法

以上是PHP模板引擎Smarty內建變數調解器的用法及實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn