
smarty 安装与入门
smarty 安装及初级使用
在 PHP 的世界里已经出现了各式各样的模板类,但就功能和速度来说 Smarty 还是一直处于
领先地位,因为 Smarty 的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。
现在就用 30 分钟让您快速入门。GGG
一. 安装GG
首先打开网页 http://smarty.PHP.net/download.PHP,下载最新版本的 Smarty。解压下载的文
件(目录结构还蛮复杂的)。接下来我演示给大家一个安装实例,看过应该会举一反三的。
(1) 我在根目录下建立了新的目录 learn/,再在 learn/里建立一个目录 smarty/。将刚才解压
缩出来的目录的 libs/拷贝到 smarty/里,再在 smarty/里新建 templates 目录,templates 里新
建 cache/,templates/,templates_c/, config/
(2) 新建一个模板文件:index.tpl,将此文件放在 learn/smarty/templates/templates 目录下,
代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Smarty</title> </head> <body> {$hello} </body> </html>
新建 index.PHP,将此文件放在 learn/下:
<?PHP
//引用类文件
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;AA
//设置各个目录的路径,这里是安装的重点
$smarty->template_dir = "smarty/templates/templates";
$smarty->compile_dir = "smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir = "smarty/templates/cache";
//smarty 模板有高速缓存的功能,如果这里是 true 的话即打开 caching,但是会造成网页不
立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;AA
$hello = "Hello World!";
//赋值
$smarty->assign("hello",$hello);
//引用模板文件
$smarty->display('index.tpl');AAA ?>AA
(3) 执行 index.php 就能看到 Hello World!了。GGGG
二. 赋值GGG