smarty基礎指南

smarty 是PHP的一個模板引擎,使得其內部程序邏輯與外部表現邏輯可分離開來,在MVC中負責實現view層的相關功能。

最新版的smarty可在官網下載,官網地址:http://www.smarty.net/

下載完成之後解壓,把裏面的libs文件夾改名爲smarty,複製到本地的web目錄下。

至於web目錄,如果本機沒有web服務器,可以下載xampp軟件,在官網可找到,安裝之後,htdocs文件夾下即爲web根目錄。


下面就可以使用smarty了,附下我做的例子,涵蓋了一些smarty的用法。

注:smarty的註釋格式爲 {* 註釋 *}

在htdocs目錄下新建一個test-smarty目錄,test.php文件放在其中,然後在test-smarty裏再建一個template目錄(存放模板)、template_c目錄(存放模板編譯的php)和cache目錄(存放緩存)。

test.php代碼:

<?php
	require('../smarty/Smarty.class.php');  //引入smarty類,也可用include_once('../smarty/Smarty.class.php');
	$smarty=new Smarty();  //實例化一個smarty類的對象
	//五配置
	$smarty->left_delimiter="{";  //左定界符,smarty處理左右定界符之間的內容
	$smarty->right_delimiter="}";  //右定界符
	$smarty->template_dir="template";  //HTML模板的地址
	$smarty->compile_dir="template_c";  //模板編譯生成php文件的地址
	$smarty->cache_dir="cache";  //緩存地址
	//緩存的兩個額外配置
	$smarty->caching=true;  //開啓緩存
	$smarty->cache_lifetime=120;  //緩存時間

	//兩方法assign()和display()
	$smarty->assign('title','news HI');  //對smarty模板中的變量賦值
	$arr=array('title'=>'smarty-study','author'=>'xiao ming');  //一維數組
	$smarty->assign('arr',$arr);
	$arr2=array(
		array('title1'=>'world1','title2'=>'world2'),
		array('title1'=>'world3','title2'=>'world4')
	);                                    //二維數組,可測試循環
	$smarty->assign('arr2',$arr2);
	$smarty->assign('time',time());  //獲取當前時間,賦值給time變量
	$smarty->assign('blank',"");  //建立一個空變量
	$smarty->assign('url',"http://www.baidu.cn");  //建立一個空變量
	$smarty->assign('long',"happy new year!
		happy new year!");  //帶換行的字符串
	$smarty->assign('score',91);  //測試條件判斷
	class myObject{
		function meth1($params){
			return $params[0].'已經'.$params[1];
		}
	}  //定義一個類
	$myobj=new myObject();  //實例化類的對象
	$smarty->assign('myobj',$myobj);
	function test($params){
		// print_r($params);  //輸出參數
		// exit;   //斷點,後面的不執行了
		$p1=$params['p1'];
		$p2=$params['p2'];
		return '傳入的參數1爲'.$p1.',傳入的參數2爲'.$p2;
	}                                           //自定義的函數
	$smarty->registerPlugin('function','f_test','test');  //註冊test函數,註冊後叫f_test
	$smarty->assign('str','hello,how are you。hello,how are you。 hello,how are you。'); //測試block插件
	$smarty->display('test.html');  //展現模板,後綴可以爲任意字符,常用html文件
?>
“兩方法”之前爲配置部分,後面爲具體的使用部分。

最後一句display方法則展現具體模板,這個模板test.html放在htdocs/test-smarty/template中。

test.html代碼:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>smarty</title>
</head>
<body>
	<p>{$title}</p><!--輸出變量-->
	<p>{$arr.title}{$arr.author}</p><!--一維數組-->
	<p>{$title|capitalize}</p><!--變量調節器-單詞首字母大寫-->
	<p>{$title|cat:' today':' happy'}</p><!--變量調節器-字符串連接-->
	<p>{$time|date_format}</p><!--變量調節器-日期格式化-->
	<p>{$blank|default:'here'}</p><!--變量調節器-變量默認值-php裏未定義變量blank時也會顯示此默認值-->
	<p>{$url|escape:'url'}</p><!--變量調節器-把url變量以url方式轉碼-->
	<p>{$title|lower}</p><!--變量調節器-轉小寫-->
	<p>{$title|upper}</p><!--變量調節器-轉大寫-->
	<p>{$long|nl2br}</p><!--變量調節器-把換行符轉爲html標籤-->
	<p>{if $score gt 90}
		優秀
		{elseif $score gt 60}
		及格
		{else}
		不及格
		{/if}
	</p>  <!--條件判斷 eq(==),neq(!=),gt(>),lt(<)-->
	<p>
		{section name=i loop=$arr2}
			{$arr2[i].title1}
			{$arr2[i].title2}
		{/section}
	</p>  <!--section 循環,除了name和loop還有其他屬性-->
	<p>
		{foreach $arr2 as $one}
			{$one.title1}
			{$one.title2}
		{foreachelse}
			no content
		{/foreach}
	</p>  <!--foreach 循環(推薦), one每次循環中取二維數組中的一維數組 -->
	<p>{include file="header.tpl" myname="kate"}</p>  <!-- 引入其它的模板文件, myname是自定義屬性, 僅僅能在hearder文件中使用 -->
	<p>{$myobj->meth1(array('蘋果','熟了'))}</p>  <!--類和對象-->
	<p>{'Y-m-d'|date:$time}</p> <!--smarty函數,使用php的內置函數date-->
	<p>{'H'|str_replace:'D':$title}</p> <!--smarty函數,使用php的內置函數str_replace,在php中是str_replace($title,'H','D')-->
	<p>{f_test p1="abc" p2="def"}</p> <!--smarty函數,自定義函數,參數p1和p2傳給函數f_test的參數中-->
	<p>{test width=150 height=200}</p> <!--smarty插件,test是function插件(自定義的)在plugins文件夾中-->
	<p>{$time|test:"Y-m-d H:i:s"}</p> <!--smarty插件,test是modifier插件(自定義的)在plugins文件夾中-->
	<p>
		{test2 replace='true' maxnum=20}
		{$str}
		{/test2}
	</p>  <!--smarty插件,test2是block插件(自定義的)在plugins文件夾中-->
</body>
</html>


如此,在瀏覽器中輸入“http://localhost/test-smarty/test.php”即可顯示結果。


代碼中最後演示了smarty插件,說明一下:

smarty插件,本質上是函數。
常用類型:
function 函數插件
modifier 修飾插件(變量調節器插件) 
block 區塊插件
使用方法:將寫好的插件放在smarty目錄下的plugins中。

上面添加的三個插件文件如下。

function.test.php:

<?php
	//自定義smarty插件
	function smarty_function_test($params){  //注意函數名的格式
		$width=$params['width'];
		$height=$params['height'];
		$area=$width*$height;
		return $area;
	}
?>

modifier.test.php:

<?php
	function smarty_modifier_test($utime,$format){
		return date($format,$utime);
	}
?>

block.test2.php:

<?php
	function smarty_block_test2($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;
	}
?>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章