简体   繁体   English

在这种情况下如何聪明地使用缓存

[英]how smarty use cache in this case

if(!$smarty->isCached("sidebar.tpl")){
$tags = $tagService->tagList();
$categories = $categoryService->categoryList();
$sidebarData = $smarty->createData();
$sidebarData->assign("tags", $tags);
$sidebarData->assign("categories", $categories);
$smarty->setCacheLifetime(3600*24);
$sidebarHtml = $smarty->fetch("sidebar.tpl", $sidebarData);

} }

I want assign $sidebarHtml to another Template, bu it seems dosen't work, I can get $sidebarHtml, through: 我想将$ sidebarHtml分配给另一个模板,但似乎不起作用,我可以通过以下方式获取$ sidebarHtml:
$smarty->fetch("sidebar.tpl")
I need $tags and $categories for 'sidebar.tpl' 我需要'sidebar.tpl'的$ tags和$ categories
or 要么
$smarty->fetch("sidebar.tpl", $sidebarData);
the $sidebarData is undefined $ sidebarData未定义

how to deal in this case 在这种情况下如何处理

This page: http://www.smarty.net/docs/en/api.is.cached.tpl 本页: http//www.smarty.net/docs/en/api.is.cached.tpl

Shows this example:
<?php
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl')) {
// do database calls, assign vars here
}

$smarty->display('index.tpl');
?>

So in your case you should put the fetch after the closing } of the iscached's if. 因此,在您的情况下,应将提取操作放在iscached的if的结束}之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM