Smarty高級應(yīng)用之緩存操作技巧分析
本文實例講述了Smarty高級應(yīng)用之緩存操作技巧。分享給大家供大家參考,具體如下:
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:
<div>{insert name="get_current_time"}</div>
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:
<div>{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:
<div align='center'> Page created: {"0"|date_format:"%D %H:%M:%S"} {dynamic} Now is: {"0"|date_format:"%D %H:%M:%S"} ... do other stuff ... {/dynamic} </div>
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,正如我們試驗中的所做的那樣
但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ù)實例
<?php $smarty->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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。
相關(guān)文章
Thinkphp 5.0實現(xiàn)微信企業(yè)付款到零錢
這篇文章主要為大家詳細介紹了Thinkphp 5.0實現(xiàn)微信企業(yè)付款到零錢,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解
這篇文章主要介紹了PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解,步驟講解的很清晰,有需要的同學(xué)可以研究下2021-03-03Laravel ORM 數(shù)據(jù)model操作教程
今天小編就為大家分享一篇Laravel ORM 數(shù)據(jù)model操作教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10WordPress偽靜態(tài)規(guī)則設(shè)置代碼實例
這篇文章主要介紹了WordPress偽靜態(tài)規(guī)則設(shè)置代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12