日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php smarty 语法,5. Smarty基本语法

發(fā)布時(shí)間:2023/12/14 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php smarty 语法,5. Smarty基本语法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

注釋

{* 注釋內(nèi)容 *}

輸出

比如,現(xiàn)在php文件中有一個(gè)數(shù)組,并且用assign方法賦值給了一個(gè)模板文件test.tpl

$arr=array('title'=>'smarty的學(xué)習(xí)', 'author'=>'小明')

$smarty->assign('arr', $arr);```

然后我們在模板中輸出:

{$arr.title} // 方法一

{$arr['title']} // 方法二```

變量調(diào)節(jié)器

變量調(diào)節(jié)器,寫在變量后面,用豎線|隔開。

1 - 首字母大寫 capitalize

示例:{$articleTitle|capitalize}

php文件:

$smarty->assign('articletitle', 'i ate an apple');

$smarty->display('test.tpl');```

tpl文件:```{$articletitle|capitalize}```

輸出:```I Ate An Apple```

**2 - 字符串連接 cat**

示例:```{$articleTitle|cat:"yesterday."}```

php文件:

$smarty->assign('articletitle', 'i ate an apple ');

$smarty->assign('yesterday', 'yesterday');

$smarty->assign('point', '.');

$smarty->display('test.tpl');tpl文件:{$articletitle|cat:"yesterday.":"point"}```

輸出:i ate an apple yesterday.

3 - 日期格式化 date_format

示例:{$yesterday|date_format}

{$yesterday|date_format:" :"}

php文件:

$smarty->assign('time', time()); //time()為php內(nèi)置函數(shù),用于獲取unix格式的時(shí)間

$smarty->display('test.tpl');```

tpl文件:```{$time|date_format}```

輸出:`Feb 07, 2017`

or

tpl文件:```{$time|date_format:"%B %e, %Y %H:%M:%S"}```

輸出:`Feb 07, 2017 08:05:08 //注意:時(shí)間不對是因?yàn)闆]有設(shè)置時(shí)區(qū),當(dāng)前時(shí)間為格林威治時(shí)間`

**4 - 為未賦值或?yàn)榭盏淖兞恐付J(rèn)值default**

示例:```{$articleTitle|default:"no title"}```

php文件:

$smarty->assign('articletitle', ' ');

$smarty->display('test.tpl');tpl文件:{$articletitle|default:"no title"}```

輸出:no title

5 - 轉(zhuǎn)碼 escape

用于html轉(zhuǎn)碼,url轉(zhuǎn)碼,在沒有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號,十六進(jìn)制轉(zhuǎn)碼,十六進(jìn)制美化,或者js轉(zhuǎn)碼。more為html轉(zhuǎn)碼。

php文件:

$smarty->assign('url', 'www.chuying-he.com');

$smarty->display('test.tpl');```

tpl文件:```{$url|escape:"url"} //escape:后面跟著的是轉(zhuǎn)碼方式```

輸出:`www.chuying-he.com`

**6 - 小寫 lower 大寫 upper**

將變量字符串小 / 大寫

示例:

```{$articletitle|lower}```

```{$articletitle|upper}```

php文件:

$smarty->assign('articletitle', 'Happy New Year');

$smarty->display('test.tpl');```

tpl文件:

{$articletitle|lower}

{$articletitle|upper}```

輸出:

happy new year

HAPPY NEW YEAR

**7 - 將所有換行符替換成
nl2br**

將所有換行符替換成
,與PHP中的nl2br()函數(shù)一樣

示例:```{$articletitle|nl2br}```

php文件:

$smarty->assign('articletitle', 'Happy

New

Year');

$smarty->display('test.tpl');```

tpl文件:

{$articletitle|nl2br}```

輸出:

Happy

New

Year

**8 - 其他函數(shù)**

可參見手冊,但原則上應(yīng)該通過php或者CSS直接處理完畢,在賦值到Smarty中,盡量少用Smarty函數(shù)

##Smarty的條件判斷語句

Smarty中,```eq```表示```==```,```neq```表示```!=```,```gt```表示```>```,```lt```表示```

{if isset($name) && $name == 'Blog'}

{* do something }

{elseif $name == $foo}

{ do something *}

{/if}

##Smarty的循環(huán)語句

**第一種循環(huán) section**

```{section}```, ```{sectionelse}``` 是Smarty的內(nèi)置函數(shù),詳細(xì)文檔點(diǎn)擊[這里](http://www.smarty.net/docs/zh_CN/language.function.section.tpl)。

php文件:

$articlelist=array(

array(

"title" => "My first article",

"author" => "Alex",

"content" => "I'm a web developer."

),

array(

"title" => "Work Note",

"author" => "John Doe",

"content" => "Where is my home?"

)

)

$smarty->assign("articlelist", $articlelist);

$smarty->display("test.tpl");

tpl文件:

{section name=article loop=$articlelist}

{$articlelist[article].title}

{$articlelist[article].author}

{$articlelist[article].content}

{/section}

輸出:

My first article Alex I'm a web developer.

Work Note John Doe Where is my home?```

第二種循環(huán) foreach

{foreach} {foreachelse}用于循環(huán)數(shù)組,語法上比{section}更加簡單清晰,并且可以使用非數(shù)字下標(biāo)。

php文件:同上

tpl文件:

{foreach item=article from=$articlelist}

{$article.title}

{$article.author}

{$article.content}

{foreachelse}

NOTHING IN ARRAY

{/foreach}

輸出:同上

注意:{foreachelse}后面的內(nèi)容會(huì)在當(dāng)前數(shù)組中沒有內(nèi)容時(shí)顯示

Smarty的文件引用

在PHP中,有兩種引入方式:include和require。在Smarty中只有include這一種。把別的模板引入進(jìn)來。

語法:{include file='page_header.tpl' sitename="sugar"}

解釋:file為要導(dǎo)入的模板名,sitename中的值會(huì)傳遞到page_header.tpl里面去,要注意的是,sitename這個(gè)值 能且只能 在page_header.tpl里面使用。假如page_header.tpl中有同名變量,該變量值會(huì)被sitename的值替代

Smarty類和對象的賦值與使用

assign除了能賦值一個(gè)變量 or 數(shù)組,還能把一個(gè)類的對象以變量的形式賦值到smarty的模板當(dāng)中去。

php文件:

class My_Object{

function methl($params){

return $params[0].' is already '.$params[1];

}

}

$myobj=new My_Object;

$smarty->assign('myobj',$myobj); // 將對象$myobj傳入test.tpl模板的名為'myobj'的對象中

$smarty->display('test.tpl');

tpl文件:

{$myobj->meth1(array('cake','done'))} // 這里的$myobj是從php文件中傳入的對象,該對象可使用類中的methl方法

輸出:

cake is already done

Smarty函數(shù)的使用

盡管Smarty中有大量的變量調(diào)節(jié)器,也還是有用戶希望使用其他方法,比如:

1. 使用php內(nèi)置函數(shù)

使用php內(nèi)置函數(shù)

php文件:

$smarty->assign('time', time());

$smarty->display('test.tpl');

tpl文件:

// date()為php的內(nèi)置函數(shù)

// 第一次嘗試:{$time|date("Y-m-d")};

// ---- 注意:該寫法被解釋成:date這個(gè)函數(shù)有兩個(gè)參數(shù),第一個(gè)參數(shù)為$time,第二個(gè)參數(shù)為"Y-m-d" ----

// 而php的date方法應(yīng)該是第一個(gè)參數(shù)為日期格式,第二個(gè)參數(shù)為unix時(shí)間戳,所以我們要變換順序如下:

{"Y-m-d"|date($time)};

輸出:

2017-02-08```

**2. 使用自定義函數(shù),并使用 ```registerPlugin``` 注冊到smarty模板里面使用**

![使用 ```registerPlugin``` ](http://upload-images.jianshu.io/upload_images/2662224-27e613ab79acb6fe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**注意**:```registerPlugin```的第一個(gè)參數(shù)除了function,還有modifier,block等,下面在smarty的插件制作中會(huì)詳細(xì)講到

php文件:

function test($params){

$p1=$params['p1'];

$p2=$params['p2'];

return 'the first para is '.p1.', the second para is '.p2;

}

// 第一個(gè)參數(shù)表示被注冊程序的類型(plugin/modifier/block),第二個(gè)參數(shù)表示注冊到smarty中后函數(shù)的名字,第三個(gè)參數(shù)表示要傳入的參數(shù)名。

$smarty->registerPlugin('function', 'f_test', 'test');

$smarty->display('test.tpl');

tpl文件:

{f_test p1='abc' p2='edf'}; // {函數(shù)名 第一個(gè)參數(shù) 第二個(gè)參數(shù)} 即,將兩個(gè)參數(shù)傳送到函數(shù)f_test中

輸出:

```the first para is abc, the second para is edf```

**3. 自定義插件**

在第一和第二種方法中,我們發(fā)現(xiàn),在最終的模板調(diào)用函數(shù)的時(shí)候,方法不同,格式也不同,這是非常不友好的書寫方式,為了解決該問題,我們需要一樣新的東西:smarty插件。

*-------- 什么是插件 --------*

插件是遵循原來的系統(tǒng)主體接口的編寫規(guī)范,編寫出來的程序,它能夠調(diào)用主系統(tǒng)的數(shù)據(jù)和函數(shù)庫。他的好處是可執(zhí)行力強(qiáng),可重復(fù)利用率高,本身的增刪改不影響主系統(tǒng)的結(jié)構(gòu)。

Smarty中也提供了這種插件機(jī)制,能夠在不改變smarty主程序的前提下,增加更多豐富的功能。 Smarty的本質(zhì)其實(shí)是function函數(shù),它的書寫方式完全遵照php的函數(shù)語法。

*-------- Smarty插件類型 --------*

- functions 函數(shù)插件

- modifiers 修飾插件

- block functions 區(qū)塊函數(shù)插件

*-------- 插件制作方法 --------*

1. 使用```registerPlugin```方法注冊寫好的自定義函數(shù)(即方法2)

2. 將寫好的插件放入Smarty解壓目錄的lib下的plugins目錄中

3. php內(nèi)置函數(shù),可以作為修飾插件(變量調(diào)節(jié)器插件)的形式在模板中使用

*-------- 在```wamp/www/smarty/smarty/plugins```目錄中,可以看到許多php文件 --------*

- 以```function```開頭類似```function.counter.php```為函數(shù)插件

- 以```modifier```開頭類似```modifier.capitalize.php```為修飾插件

- 以```block```開頭類似```block.textformat.php```為區(qū)塊函數(shù)插件

注意:命名規(guī)則為```插件類型.插件名.php```

*-------- functions 函數(shù)插件舉例 --------*

// 名為 function.test1.php 的函數(shù)插件

// 命名規(guī)則: smarty_function_插件名(要和文件中的插件名一樣)

function smarty_function_test1($params){

$width = $params['width'];

$height = $param['height'];

$area = $width * $height;

return $area;

}

?>```

// test.php文件

$smarty->display('area.tpl');

// area.tpl 模板文件

//smarty對該語句的處理是:調(diào)用test函數(shù),然后將兩個(gè)參數(shù)放在一個(gè)數(shù)組里面?zhèn)鬟f到test函數(shù)中array('width'=>150, 'height'=>200)

{test1 width=150 height=200}

-------- modifier 函數(shù)插件舉例 --------

// 名為modifier.test2.php的modifier插件

// 命名規(guī)則: smarty_modifier_插件名(要和文件中的插件名一樣)

// 該modifier定義兩個(gè)參數(shù),第一個(gè)為unic時(shí)間戳,第二個(gè)為時(shí)間形式

function smarty_modifier_test2($utime, $format){

return date($format, $utime);

}

?>```

// test.php文件

$smarty->assign('time',time());

$smarty->display('datetime.tpl');```

// datetime.tpl 模板文件

{$time|test2:'Y-m-d H:i:s'}```

*-------- block 函數(shù)插件舉例 --------*

// 名為block.test3.php的block插件

function smarty_block_test3($params, $content){

$replace=$params['replace'];

$maxnum=$params['maxnum'];

if($replace=='true'){

$content=str_replace(',', ',', $content); // 將中文的逗號替換成英文的逗號

$content=str_replace('。','.', $content); // 將中文的句號替換成英文的句號

}

$content=substr($content, 0, $maxnum);

return $content;

}

?>```

// test.php文件

$smarty->assign('str','Hello, my name is HanMeiMei。What is your name?');

$smarty->display('content.tpl');```

// content.tpl 模板文件

{test3 replace='true' maxnum=29}

{$str}

{/test3}```

總結(jié)

以上是生活随笔為你收集整理的php smarty 语法,5. Smarty基本语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。