Smarty中緩存操作技巧的示例分析-創(chuàng)新互聯(lián)
這篇文章將為大家詳細講解有關(guān)Smarty中緩存操作技巧的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
smarty緩存控制
smarty提供了強大的緩存功能。但有時我們并不希望整篇文檔都被緩存,而是有選擇的緩存某一部分內(nèi)容或某一部分內(nèi)容不被緩存。例如你在頁面上端使用一個帶有廣告條位置的模板,廣告條可以包含任何HTML、圖象、FLASH等混合信息. 因此這里不能使用一個靜態(tài)的鏈接,同時我們也不希望該廣告條被緩存. 這就需要在 insert 函數(shù)指定,同時需要一個函數(shù)取廣告條的內(nèi)容信息。smarty也提供了這種緩存控制能力。
我們可以使用{insert}使模板的一部分不被緩存
可以使用$smarty->register_function($params,&$smarty)阻止插件從緩存中輸出,
還可以使用$smarty->register_block($params,&$smarty)使整篇頁面中的某一塊不被緩存。
下面我們真對一個簡單需求,分別說明這三種控制緩存輸出的方法。
需求:被緩存的文檔中當(dāng)前時間不被緩存,隨每次刷新而變化。
1、使用insert函數(shù)使模板的一部分不被緩存
index.tpl:
{insert name="get_current_time"}
index.php
function insert_get_current_time(){ return date("Y-m-d H:m:s"); } $smarty=new smarty(); $smarty->caching = true; if(!$smarty->is_cached()){ ....... } $smarty->display('index.tpl');
注解:
定義一個函數(shù),函數(shù)名格式為:
inser_name(array $params, object &$smarty),
函數(shù)參數(shù)可選的,如果在模板的insert方法中需要加入其他屬性,就會作為數(shù)組傳遞給用戶定義的函數(shù)。
如:
{insert name='get_current_time' local='zh'}
在get_current_time函數(shù)中我們就可以通過$params['local']來獲得屬性值。
如果在get_current_time函數(shù)中需要用到當(dāng)前smarty對象的方法或?qū)傩裕涂梢酝ㄟ^第二個參數(shù)獲得。
這時你會發(fā)現(xiàn)index.tpl已被緩存,但當(dāng)前時間卻隨每次刷新在不斷變化。
2、使用register_function阻止插件從緩存中輸出
index.tpl:
{current_time}{/div}
index.php:
function smarty_function_current_time($params, &$smarty){ return date("Y-m-d H:m:s"); } $smarty=new smarty(); $smarty->caching = true; $smarty->register_function('current_time','smarty_function_current_time',false); if(!$smarty->is_cached()){ ....... } $smarty->display('index.tpl');
注解:
定義一個函數(shù),函數(shù)名格式為:smarty_type_name($params, &$smarty)
type為function
name為用戶自定義標簽名稱,在這里是{current_time}
兩個參數(shù)是必須的,即使在函數(shù)中沒有使用也要寫上。兩個參數(shù)的功能同上。
3、使用register_block使整篇頁面中的某一塊不被緩存
index.tpl:
Page created: {"0"|date_format:"%D %H:%M:%S"} {dynamic} Now is: {"0"|date_format:"%D %H:%M:%S"} ... do other stuff ... {/dynamic}
index.php:
function smarty_block_dynamic($param, $content, &$smarty) { return $content; } $smarty = new Smarty; $smarty->caching = true; $smarty->register_block('dynamic', 'smarty_block_dynamic', false); if(!$smarty->is_cached()){ ....... } $smarty->display('index.tpl');
注解:
定義一個函數(shù),函數(shù)名格式為:smarty_type_name($params, &$smarty)
type為block
name為用戶自定義標簽名稱,在這里是{dynamic}
兩個參數(shù)是必須的,即使在函數(shù)中沒有使用也要寫上。兩個參數(shù)的功能同上。
4、總結(jié)
(1)對緩存的控制能力:
使用register_function和register_block能夠方便的控制插件輸出的緩沖能力,可以通過第三個參數(shù)控制是否緩存,默認是緩存的,需要我們顯示設(shè)置為false,正如我們試驗中的所做的那樣
復(fù)制代碼 代碼如下:
$smarty->register_function('current_time','smarty_function_current_time',false);
但insert函數(shù)默認是不緩存的。并且這個屬性不能修改。從這個意義上講insert函數(shù)對緩存的控制能力似乎不如register_function和register_block強。
(2)使用方便性:
但是insert函數(shù)使用非常方便。不用顯示注冊,只要在當(dāng)前請求過程中包含這個函數(shù)smarty就會自動在當(dāng)前請求的過程中查找指定的函數(shù)。
當(dāng)然register_function也可以做到不在運行時顯示注冊。但是那樣做的效果跟其他模版函數(shù)一樣,統(tǒng)統(tǒng)被緩存,并且不能控制。
如果你使用在運行時顯示調(diào)用register_function注冊自定義函數(shù),那么一定要在調(diào)用is_cached()方法前完成函數(shù)的注冊工作。
否則在is_cached()這一步緩存文檔將因為找不到注冊函數(shù)而導(dǎo)致smarty錯誤
Smarty用戶自定義函數(shù)實例
register_function('date_now', 'print_current_date'); function print_current_date($params, &$smarty) { if(empty($params['format'])) { $format = "%b %e, %Y"; } else { $format = $params['format']; } return strftime($format,time()); } ?>
在模板中使用
{date_now} {* or to format differently *} {date_now format="%Y/%m/%d"}
關(guān)于“Smarty中緩存操作技巧的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
網(wǎng)站標題:Smarty中緩存操作技巧的示例分析-創(chuàng)新互聯(lián)
分享地址:http://www.xueling.net.cn/article/dpspid.html