欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP模板引擎Smarty內(nèi)置變量調(diào)解器用法詳解

 更新時間:2016年04月11日 11:07:03   作者:ruxingli  
這篇文章主要介紹了PHP模板引擎Smarty內(nèi)置變量調(diào)解器用法,結(jié)合實例形式詳細分析了Smarty中的常用內(nèi)置變量調(diào)節(jié)器定義與使用技巧,需要的朋友可以參考下

本文實例講述了PHP模板引擎Smarty內(nèi)置變量調(diào)解器用法。分享給大家供大家參考,具體如下:

Smarty 中的變量調(diào)解器相當(dāng)于函數(shù),其調(diào)用方式為:通過 "|" 后面直接跟調(diào)解器函數(shù)名,如果有參數(shù),得加在 ":" 后面,多個參數(shù)的話,累加即可。

下面為您介紹 Smarty 中內(nèi)置的變量調(diào)解器:

1、capitalize

將變量里的所有單詞首字大寫。參數(shù)值 boolean 型決定帶數(shù)字的單詞,首字是否大寫。默認不大寫

index.php

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

index.html(模板文件)

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

結(jié)果為:Hello World wor2ld!!!、 Hello World Wor2Ld!!!

2、count_characters

計算變量里的字符數(shù),該調(diào)解器默認不計算空格(空格、制表符、回車…)只計算字符的個數(shù),并且能很好的支持中文字符計算;如果添加參數(shù) true ,則計算空格。

index.html

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

結(jié)果為:13、14

3、cat

連接字符串,將cat里的值連接到給定的變量后面。

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

結(jié)果為:hello world!!! Happy new year.

4、count_paragraphs

計算段數(shù),計算變量里的段落數(shù)量,完美支持中文段落。

index.php

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

index.html

<{$str|count_paragraphs}>

結(jié)果為:3

5、count_sentences

計算句數(shù),計算變量里句子的數(shù)量。注:只支持英文語句,不支持中文。

index.php

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

index.html

<{$str|count_sentences}>

結(jié)果為:2

6、count_words

計算詞數(shù),計算變量里的詞數(shù)。

index.php

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

index.html

<{$str|count_words}>

結(jié)果為:12

7、date_format

日期格式化,具體參數(shù)很多,這里只舉例中國式日期格式

index.php

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

index.html

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

結(jié)果為:2012-01-26 14:37:22

8、default

默認,為空變量設(shè)置一個默認值,當(dāng)變量為空或者未分配的時候,將由給定的默認值替代輸出。

index.php

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

index.html

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

結(jié)果為:默認輸出...、沒有定義,默認輸出...

9、escape

轉(zhuǎn)碼,用于 html 轉(zhuǎn)碼,url 轉(zhuǎn)碼,在沒有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號,十六進制轉(zhuǎn)碼,十六進制美化,或者 javascript 轉(zhuǎn)碼,默認是html轉(zhuǎn)碼

index.php

$html = <<Google
html;
$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 轉(zhuǎn)碼:<{$html|escape:"html"}>
URL 轉(zhuǎn)碼:<{$url|escape:"url"}>
JS 轉(zhuǎn)碼:<{$js|escape:"javascript"}>

結(jié)果為:

HTML 轉(zhuǎn)碼:Google
URL 轉(zhuǎn)碼:http%3A%2F%2Fwww.google.com.hk
JS 轉(zhuǎn)碼:

10、indent

縮進,每行縮進字符串,第一個參數(shù)指定縮進多少個字符串,默認是四個字符;第二個參數(shù),指定縮進用什么字符代替。

11、lower

小寫,將變量字符串小寫。

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

12、upper

大寫,將變量改為大寫。

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

13、nl2br

換行符替換成

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

14、regex_replace

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

index.php

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

index.html

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

結(jié)果為:http://www.baidu.com

15、replace

替換,簡單的搜索和替換字符串。

16、spacify

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

index.php

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

index.html

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

結(jié)果為:h^^e^^l^^l^^o^^ ^^w^^o^^r^^l^^d^^!^^!^^!

17、string_format

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

index.php

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

index.html

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

結(jié)果為:23.58、23

18、strip

替換所有重復(fù)的空格、換行、tab 為單個

index.php

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

index.html

<{$str|strip:" "}>

結(jié)果為:Grandmother of eight makes hole in one.

源代碼:

Grandmother&nbsp;of&nbsp;eight&nbsp;makes&nbsp;hole&nbsp;in&nbsp;one.

19、strip_tags

去除在<和>之間的所有標(biāo)簽,包括<和>。

index.php

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

index.html

<{$str|strip_tags}>

結(jié)果為:Google(源代碼也是 Google,去掉了標(biāo)簽和標(biāo)簽)

20、truncate

截取,截取字符串開始的一段.默認是80個,你可以指定第二個參數(shù)作為在截取的那段字符串后加上什么字符,默認情況下,smarty會截取到一個詞的末尾,如果你想要精確的截取多少個字符,把第三個參數(shù)改為"true" 。

index.php

復(fù)制代碼 代碼如下:
$tpl->assign('str', '從前有座山,山上有座廟。廟里有一個老和尚和一個小和尚...');

index.html

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

結(jié)果為:從前有座山,山...

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論