一: Smarty介绍
使用Smarty 要掌握一个原则:将程序应用逻辑与网页呈现逻辑明确地分离。就是说 PHP 程序里不要有太多的 HTML 码。程序中只要决定好那些变量要塞到模版里,让模版自己决定该如何呈现这些变量
在 Smarty 中,一切以变量为主,所有的呈现逻辑都让模版自行控制。因为 Smarty 会有自己的模版语言,所以不管是区块是否要显示还是要重复,都是用 Smarty 的模版语法 (if, foreach, section) 搭配变量内容作呈现。这样一来感觉上好象模版变得有点复杂,但好处是只要规划得当, PHP 程序一行都不必改。
二: Smarty模板运行示意图:
三: Smarty安装配置:
1. 官方网站下载:http://www.smarty.net/ 最新版本是:Smarty-2.6.21
2. 解压Smarty-2.6.21,里面的libs目录下的文件是我们需要的
3. 在网站根目录下建立项目文件夹smartyDmoe/, 将libs文件夹整个考到smartyDmoe目录下。
4. smartyDmoe/下新建6个文件夹:
cache
configs
templates //模版文件
templates_c //Smarty 编译过的档案
includes //用来放置一些 function 、 sql 檔
modules //是用来放置程序模块的,如此一来便不会把程序丢得到处都是,整体架构一目了然。
5. test.htm,保存在 templates 目录下面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>
6. 建立文件模板配置文件: main.php,保存在项目主目录smartyDmoe/ 下面
<?php
include "libs/Smarty.class.php";
@define('__SITE_ROOT', str_replace("\\","/",dirname(__FILE__)));
$tpl = new Smarty();
$tpl->template_dir = __SITE_ROOT . "/templates/";
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '{';
$tpl->right_delimiter = '}';
?>
//left_delimiter
和right_delimiter
两行其实可以不用写的,因为在Smarty.class.php中已经赋值了。
7. 建文件test.php文件,放在项目主目录smartyDmoe/ 下面
<?php
require "config.php";
$tpl->assign("title", "smarty配置");
$tpl->assign("content", "smarty配置成功了");
$tpl->display('test.htm');
?>
8. 在浏览器输入http://localhost/smartyDmoe/test.php
显示: smarty配置成功了
9. 为了能在网站全局使用Smarty技术,我们可以修改PHP.inc里面的
include_path = ".;d:\php\includes"
改为:
include_path = ".;d:\php\includes;d:\web\smartyDmoe\libs"
下面这句就不用再写
最后整理一下整个 Smarty 程序撰写步骤:
Step 1. 加载 Smarty 模版引擎。
Step 2. 建立 Smarty 对象。
Step 3. 设定 Smarty 对象的参数。
Step 4. 在程序中处理变量后,再用 Smarty 的 assign 方法将变量置入模版里。
Step 5. 利用 Smarty 的 display 方法将网页秀出。
四:控制模板的内容,重复区域
1.Smarty 样板中,重复一个区块有两种方式: foreach 及 section, 这两个函数的输出都是一样的
(1. foreach 要以巢状处理的方式来呈现我们所 assign 的两层数组变量.
(2. section 则以「主数组[循环名称].子数组索引」即可将整个数组呈现出来
其中foreach函数和PHP中的foreach函数是一样的.
section 则是 Smarty 为了处理如数组变量所发展出来的叙述。
2. 数组index索引要从0开始的,否则section输出时将不输出最后一个索引值,如果index索引不是整数的话,section将不予检索。
3. 实例:
(1. test2.php 代码:
<?php
require "config.php";
//index索引要从0开始的,否则section输出时将不输出最后一个索引值,如果index索引不是整数的话,section将不予检索
//$array1 = array(1=>"中国", 2=>"日本", 3=>"美国", 4=>"俄罗斯");
//$array1 = array("a"=>"中国", "b"=>"日本", "c"=>"美国", "d"=>"俄罗斯");
$array1 = array(=>"中国", 2=>"日本", 3=>"美国", 4=>"俄罗斯");
$tpl->assign("array1", $array1);
$array2 = array(
array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"),
array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"),
array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"),
array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"),
);
$tpl->assign("array2", $array2);
$tpl->display("test2.html");
?>
(2. test2.html 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试重复区块循环输出</title>
</head>
<body>
<!--用foreach来输出array1-->
用foreach来输出array1--->
{foreach item=arr1 from=$array1}
{$arr1}
{/foreach}
<br>
<!--用section来输出array1-->
用section来输出array1--->
{section name=sec1 loop=$array1}
{$array1[sec1]}
{/section}
<br>
<!--用foreach来输出array2-->
用foreach来输出array2---><br>
{foreach item=index2 from=$array2}
{foreach key=key2 item=item2 from=$index2}
{$key2}: {$item2}
{/foreach}
<br>
{/foreach}
<br>
<!--用section来输出array2-->
用section来输出array2---><br>
{section name=sec2 loop=$array2}
index1: {$array2[sec2].index1}
index2: {$array2[sec2].index2}
index3: {$array2[sec2].index3}
<br>
{/section}
</body>
</html>
(3. 运行结果:
测试重复区块循环输出测试重复区块循环输出用foreach来输出array1---> 中国 日本 美国 俄罗斯
用section来输出array1---> 中国 日本 美国 俄罗斯
用foreach来输出array2--->
index1: data1-1 index2: data1-2 index3: data1-3
index1: data2-1 index2: data2-2 index3: data2-3
index1: data3-1 index2: data3-2 index3: data3-3
index1: data4-1 index2: data4-2 index3: data4-3
用section来输出array2--->
index1: data1-1 index2: data1-2 index3: data1-3
index1: data2-1 index2: data2-2 index3: data2-3
index1: data3-1 index2: data3-2 index3: data3-3
index1: data4-1 index2: data4-2 index3: data4-3