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;
	}
?>





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