is_cached

is_cached -- 

bool is_cached ( string template [, string cache_id [, string compile_id]])

This returns TRUE if there is a valid cache for this template. This only works if caching is set to true.

如果某樣版有一個合法的快取的話則此函式將傳回 TRUE,且此函式只在 caching 的值為 true 時才會有動作。

Example 13-1. is_cached

<?php
$smarty
->caching = true;

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

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

You can also pass a cache id as an optional second parameter in case you want multiple caches for the given template.

你可以使用非必要的第二個參數 cache id,使用的情況是在當你所指定的樣版有多個 快取時。

You can supply a compile id as an optional third parameter. If you omit that parameter the persistent $compile_id is used.

你也可以使用非必要的第三個參數 compile id,使用的情況是在當你不知道有屬於永 久變數的 $compile_id 可使用。

If you do not want to pass a cache id but want to pass a compile id you have to pass null as cache id.

如果你不想傳一個 cache id 但是想傳一個 compile id 的話,你可以傳一個 null 作為 cache id。

Example 13-2. 使用 is_cached 在多個快取的樣版

<?php
$smarty
->caching = true;

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

$smarty->display("index.tpl", "FrontPage");
?>

Technical Note: If is_cached returns true it actually loads the cached output and stores it internally. Any subsequent call to display() or fetch() will return this internally stored output and does not try to reload the cache file. This prevents a race condition that may occur when a second process clears the cache between the calls to is_cached and to display in the example above. This also means calls to clear_cache() and other changes of the cache-settings may have no effect after is_cached returned true.

技術提醒: 如果 is_cached 回傳 true 的話則表示已載入輸出的快取且將其儲存在電腦內部。後續呼叫類似 display()fetch() 函式的話將會回傳內部儲存的輸出快取,而不會重新載入快取檔。此在避免當在呼叫 is_cached 函式與使用上述兩個函式展示輸出內容時,中間夾著呼叫清除快取的程序此一極少可 能發生的狀況。此代表在 is_cached 回傳 true 後,呼叫 clear_cache() 函式或其他會改變快取設定的函式將會沒有作用。