每次调用PHP应用程序,Smarty 会试着查看自上次编译时间以来,当前模板是否被修改过.如果修改过了,她会重新编译那个模板.如果模板还没有被编译过,她将编译模板而不管编译检查如何设置.默认情况下编译检查这个变量设置为true.一旦一个应用程序投入产品中(模板将不会修改了),就不再需要编译检查这一步了.为了最大性能,确定将$compile_check设为"false".注意:如果设为了"false",虽然模板文件被修改,但你不会看到修改结果,因为模板没有得到重新编译.如果启动了缓存和编译检查,一旦有关模板文件或配置文件被更新,缓存文件将会重建.
Q: How do I generate different cache files per template based on arguments
passed to the page?
A: Use your $REQUEST_URI as the cache_id when fetching the page:
global $REQUEST_URI; // if not already present
$smarty->display('index.tpl',$REQUEST_URI);
This will create a separate cache file for each unique URL when you call
index.tpl. See the documentation for display() and fetch()
通过给display()第二个参数为$REQUEST_URI 来为不同的动态页面建立cache
技术提示:要注意从客户端(web浏览器)传值到Smarty(或任何PHP应用程序)的过程。尽管上面的例子用
article_id从URL传值看起来很方便,却可能有糟糕的后果[安全问题]。cache_id被用来在文件系统里创建
目录,如果用户想为article_id赋一个很大的值,或写一些代码来快速发送随机的article_ids,就有可能会
使服务器出现问题。
怎么在非缓存模板里面包换,缓存模板
Q: How do I include cached template(s) within a non-cached template?
A: One way to do it:
$smarty->caching = true;
$tpl1 = $smarty->fetch("internal1.tpl");
$tpl2 = $smarty->fetch("internal2.tpl");
$tpl3 = $smarty->fetch("internal3.tpl");
$smarty->assign("tpl1_contents",$tpl1);
$smarty->assign("tpl2_contents",$tpl2);
$smarty->assign("tpl3_contents",$tpl3);
$smarty->caching = false;
$smarty->display('index.tpl');
index.tpl
---------
<table>
<tr>
<td>{$tpl1_contents}</td>
<td>{$tpl2_contents}</td>
<td>{$tpl3_contents}</td>
</tr>
</table>
Another approach:
You could write a custom insert function to fetch your internal
templates:
<table>
<tr>
<td>{insert name=fetch_tpl tpl="internal1.tpl"}</td>
<td>{insert name=fetch_tpl tpl="internal2.tpl"}</td>
<td>{insert name=fetch_tpl tpl="internal3.tpl"}</td>
</tr>
</table>