index.php
<?php
/* //直接在本文件中配置
//库文件包含
define('SMARTY_DIR','D:/APMServ5.2.6/www/htdocs/smartydemo/smarty/');
require(SMARTY_DIR.'smarty.class.php');
//配置
$smarty = new Smarty;
$smarty->compile_dir = SMARTY_DIR.'templates_c/';
$smarty->cache_dir = AMARTY_DIR.'cache/';
$smarty->template_dir = 'templates/';
$smarty->config_dir = 'config/';
$this->caching = true;
//变量传值
$smarty->assign('name', 'phf');
//显示模板文件
$smarty->display('index.tpl');
//$smarty->display('index.tpl', 2); //多份缓存
*/
/* //取出生成的文件
$file = $smarty->fetch('index.tpl');
echo $file;
*/
/*
//单个文件的cache时间设置
$smarty->caching = 2; // lifetime is per cache
$smarty->cache_lifetime = 300;
$smarty->display('index.tpl');
*/
// 实初化配置的进一步类封装
require_once('smartydemo.class.php');
$smarty = new Smarty_demo;
$smarty->assign('name', 'phf');
//$smarty->display('index.tpl');
$smarty->display('index.tpl', 2);
smartydemo.class.php
<?php
//
define('SMARTY_DIR','D:/APMServ5.2.6/www/htdocs/smartydemo/smarty/');
require(SMARTY_DIR.'smarty.class.php');
class Smarty_demo extends Smarty
{
function __construct()
{
Smarty::__construct();
$this->compile_dir = SMARTY_DIR.'templates_c/';
$this->cache_dir = SMARTY_DIR.'cache/';
$this->template_dir = 'templates/';
$this->config_dir = 'config/';
$this->caching = true;
$this->cache_lifetime = 1;
$this->assign('app_name','smartydemo');
}
}
index.tpl
{* smarty *}
<h1>{$app_name}</h1>
hello {$name}!;