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

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

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

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

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

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

1、capitalize

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

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

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

index.html

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

結(jié)果為:13、14

3、cat

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

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

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

4、count_paragraphs

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

index.php

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

index.html

<{$str|count_paragraphs}>

結(jié)果為:3

5、count_sentences

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

index.php

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

index.html

<{$str|count_sentences}>

結(jié)果為:2

6、count_words

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

index.php

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

index.html

<{$str|count_words}>

結(jié)果為:12

7、date_format

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

index.php

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

index.html

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

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

8、default

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

index.php

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

index.html

<{$str|default:'默認(rèn)輸出...'}>、<{$string|default:'沒(méi)有定義,默認(rèn)輸出...'}>

結(jié)果為:默認(rèn)輸出...、沒(méi)有定義,默認(rèn)輸出...

9、escape

轉(zhuǎn)碼,用于 html 轉(zhuǎn)碼,url 轉(zhuǎn)碼,在沒(méi)有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號(hào),十六進(jìn)制轉(zhuǎn)碼,十六進(jìn)制美化,或者 javascript 轉(zhuǎn)碼,默認(rè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

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

11、lower

小寫(xiě),將變量字符串小寫(xiě)。

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

12、upper

大寫(xiě),將變量改為大寫(xiě)。

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

13、nl2br

換行符替換成

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

14、regex_replace

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

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

替換,簡(jiǎn)單的搜索和替換字符串。

16、spacify

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

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

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

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 為單個(gè)

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

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

index.php

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

index.html

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

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

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

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

相關(guān)文章

最新評(píng)論