2009-06-06 77 views
2

我在我的項目中使用Smarty時,啓用緩存似乎不起作用。 我使用以下結構:Smarty緩存不工作?

的index.php - 顯示器(在index.tpl)

在index.tpl - {包括 文件= $ page_center}

模塊=產品 - $ smarty-> assign(「page_center」, 「product.tpl」);

在product.php中,模板product.tpl必須加載到index.tpl的中心。 當我啓用緩存時,它仍然顯示默認內容not product.tpl。當緩存被禁用時,它工作正常。啓用緩存時有什麼問題?

回答

7

您將需要使用一個唯一的緩存ID爲每個頁面,使這項工作正確:

$cacheID = 'some_unique_value_for_this_page'; 
$smarty->assign('page_center', 'product.tpl'); 
$smarty->display('index.tpl', $cacheID); 

給你的問題給的例子,它可能是有意義的,從查詢中使用的模塊名稱字符串作爲緩存ID的基礎。

有關於Smarty的手動的更多信息:http://www.smarty.net/manual/en/api.display.php

+0

好的。我測試它,但它不能幫助我,不幸的是,每次模塊調用一個新的cach文件時,都會在cach目錄中創建,與我使用$ smarty-> force_compile相同。這是不正確的方式來捕捉我的想法。 – mehdi 2009-06-07 14:55:14

0

你需要創建一個動態模塊!

function smarty_block_dynamic($param, $content, &$smarty) { 
    return $content; 
} 

然後

$smarty = new Smarty 
$smarty->register_block('dynamic', 
         'smarty_block_dynamic', 
         false /* this block wont be cached */); 

和您的TPL

Hello {$name} 

this is your las update 

{/dyamic} 
{include file="some/dynamic/thing.tpl"} 
{/dynamic} 
0

試圖解決Smarty的緩存問題之前,也就是說,如果緩存沒有發生,請檢查您的templates_c文件夾具有讀取和寫入全部權限