一、建立一个php Project,文件目录如下图
二、文件说明
1、libs:是Smarty类库
2、tpl/cache_dir:存放缓存模板
3、tpl/compile_dir:存放编译后模板文件
4、tpl/config_dir:存放特殊配置文件
5、tpl/template_dir:存放模板文件
6、smarty.php文件里 new 出了一个 Smarty类对象,并设定各对象的属性值,如下代码
require 'libs/Smarty.class.php';//加载Smarty.class.php文件
define('SITE_ROOT','./tpl/');//定义一个常量
$tpl = new Smarty();
$tpl->template_dir = SITE_ROOT . 'template_dir';//存模板文件
$tpl->compile_dir = SITE_ROOT . 'compile_dir';//存编译过的模板文件
$tpl->config_dir = SITE_ROOT . 'config_dir';//存特殊配置文件
$tpl->cache_dir = SITE_ROOT . 'cache_dir';//存Smarty缓存文件
$tpl->caching = 1;//启用缓存
$tpl->cache_lifetime = 60*60*24;//缓存时间1天
$tpl->left_delimiter = '
$tpl->right_delimiter = '}>';//右结束符
7、index.php文件 首页 代码如下
require 'smarty.php';
$tpl->assign('title','title测试');
$tpl->assign('content','content测试');
$tpl->display('template.html');
8、tpl/template_dir/template.html 这是一个模板文件 代码如下