Smarty模板引擎的使用

Smarty模板引擎的使用

Smarty是PHP中一个基于MVC模式的模板引擎。

Download: http://www.smarty.net/download

 

特点

1、  最快速度的程序开发模板

2、  自定义模板的界定符

3、  可以使用条件判断语句if/elseif/else/endif

4、  内建缓存支持

5、  可以自定义插件.

 

Smarty其实很简单,目前的3.1.14版本中的一个Demo的目录如下:



 

只有简单的几个文件夹。

 

Index.php

<?php
 /**
 * Example Application

 * @package Example-application
 */
 
//载入Smarty的核心类库文件
require('libs/Smarty.class.php');
//实例化一个Smarty
$smarty = new Smarty;



//$smarty->force_compile = true;
//调试模式,会弹出调试框,查看变量值
$smarty->debugging = true;
 //缓存模式
$smarty->caching = true;
 //缓存时间 
$smarty->cache_lifetime = 120;

//定义一个数据的MAP,将数据发送到模板中去
$smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill",true);
$smarty->assign("FirstName",array("John","Mary","James","Henry"));
$smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
	  array("I", "J", "K", "L"), array("M", "N", "O", "P")));

$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
	  array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));

$smarty->assign("option_values", array("NY","NE","KS","IA","OK","TX"));
$smarty->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas"));
$smarty->assign("option_selected", "NE");

//显示一个模板
$smarty->display('index.tpl');
?>




 

 

使用的都是默认的各个目录文件夹,显示如资源列表图。

当然,还有很多的参数设置,可以参考Smarty.class.php文件,不做详细介绍了。



index.tpl 中的解析


 

{*这样的是注释*}
{* {} 是界定符 *}

{*载入 配置文件 *}
{config_load file="test.conf" section="setup"}

{* 载入一个文件,并传入一个变量 title = foo *}
{include file="header.tpl" title=foo}

<PRE>

{* ##是从config 文件读取的配置数据 *}
{if #bold#}<b>{/if}
{* 
  ‘|’ 是针对变量的修饰符,
  capitalize   首字母大写
  upper		   全部大写
  lower        全部小写
  nl2br        将换行符换位<br/>
  date_format:"%Y-%M-%D"   格式化时间
  replace:"value1":"value2" 替换,使用value2替换value1
  dafault      默认值
  cat:"characters"    追加字符
  .....
*}
Title: {#title#|capitalize}
{if #bold#}</b>{/if}

The current date and time is {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

The value of global assigned variable $SCRIPT_NAME is {$SCRIPT_NAME}

{*
{$smarty.server.SERVER_NAME}   服务器 地址
*}
Example of accessing server environment variable SERVER_NAME: {$smarty.server.SERVER_NAME}

The value of {ldelim}$Name{rdelim} is <b>{$Name}</b>
{*
{ldelim}   左界定符
{rdelim}   右界定符
*}
variable modifier example of {ldelim}$Name|upper{rdelim}

<b>{$Name|upper}</b>


An example of a section loop:

{section name=outer 
loop=$FirstName}
{* 没两次 交替 一次*}
{if $smarty.section.outer.index is odd by 2}
	{$smarty.section.outer.rownum} . {$FirstName[outer]} {$LastName[outer]}
{else}
	{$smarty.section.outer.rownum} * {$FirstName[outer]} {$LastName[outer]}
{/if}
{sectionelse}
	none
{/section}

An example of section looped key values:
{* 类似 泛型的枚举  循环*}
{section name=sec1 loop=$contacts}
	phone: {$contacts[sec1].phone}<br>
	fax: {$contacts[sec1].fax}<br>
	cell: {$contacts[sec1].cell}<br>
{/section}
<p>

testing strip tags
{strip}
<table border=0>
	<tr>
		<td>
			<A HREF="{$SCRIPT_NAME}">
			<font color="red">This is a  test     </font>
			</A>
		</td>
	</tr>
</table>
{/strip}

</PRE>

This is an example of the html_select_date function:

{* 时间选择器*}
<form>
{html_select_date start_year=1998 end_year=2010}
</form>

This is an example of the html_select_time function:

<form>
{html_select_time use_24_hours=false}
</form>

This is an example of the html_options function:

{* option 枚举*}
<form>
<select name=states>
{html_options values=$option_values selected=$option_selected output=$option_output}
</select>
</form>

{include file="footer.tpl"}

 



在服务器上显示的  index.php就显示如下了


 

更多的详细介绍,查看官网http://www.smarty.net/docs/zh_CN/


posted on 2013-09-02 19:36  you Richer  阅读(322)  评论(0编辑  收藏  举报