接着昨天的学习:
取得输出的结果: string fetch (string template)
返回一个模板的输出内容(html代码),但不是直接显示出来,需要指定一个合法的模板资源的类型和路径
//捕获输出
$output = $smarty->fetch("index.tpl");
// 对将要输出的内容进行处理
do something with $output here
//输出
echo $output;
设置缓存:$caching = true(或 1)
建立缓存后,display函数会把状态返回没缓存的状态,把输出拷贝并保存到$cache_dir.下次调用display函数,保存的缓存会被再用来代替原来的模板。
//设置缓存时间为5分钟
$smarty->cache_lifetime = 300;
设置单个缓存会话期:
$smarty->caching = 1;
$smarty->cache_lifetime = 300;//第一次设置缓存
编译后会在cache_dir里生成一个缓存文件,这个文件里包含有此次生成缓存文件时的cache_lifetime,就是300秒;
然后修改
$smarty->caching = 2;
$smarty->cache_lifetime = 5;//第二次设置缓存
清除缓存:清除单个和所有
//只清除index.tpl
$smarty->clear_cache('index.tpl');
//清除所有缓存
$smarty->clear_all_cache();