|
導讀網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立... 網頁的本質就是超級文本標記語言,通過結合使用其他的Web技術(如:腳本語言、公共網關接口、組件等),可以創造出功能強大的網頁。因而,超級文本標記語言是萬維網(Web)編程的基礎,也就是說萬維網是建立在超文本基礎之上的。超級文本標記語言之所以稱為超文本標記語言,是因為文本中包含了所謂“超級鏈接”點。 本篇文章給大家帶來的內容是關于CI框架下smarty3的整合步驟(附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。1 下載smarty3并將libs文件放在框架libraries目錄下重命名為smarty <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'libraries/smarty/Smarty.class.php'); //這里指定Smarty.class.php的存放位置
class Ci_smarty extends Smarty
{
protected $ci;
public function __construct()
{
parent::__construct();
$this->ci = & get_instance();
$this->ci->load->config('smarty');//加載smarty的配置文件
$this->cache_lifetime =$this->ci->config->item('cache_lifetime');
$this->caching = $this->ci->config->item('caching');
$this->config_dir = $this->ci->config->item('config_dir');
$this->template_dir = $this->ci->config->item('template_dir');
$this->compile_dir = $this->ci->config->item('compile_dir');
$this->cache_dir = $this->ci->config->item('cache_dir');
$this->use_sub_dirs = $this->ci->config->item('use_sub_dirs');
$this->left_delimiter = $this->ci->config->item('left_delimiter');
$this->right_delimiter = $this->ci->config->item('right_delimiter');
}
}3 在框架config目錄下創建smarty.php,代碼如下 <?php
$config['cache_lifetime'] = 3600;//緩存失效
$config['caching'] = true;//開啟緩存
$config['template_dir'] = APPPATH .'views';
$config['compile_dir'] = APPPATH .'views/template_c';
$config['cache_dir'] = APPPATH . 'views/cache';
$config['config_dir'] = APPPATH . 'views/config';
$config['use_sub_dirs'] = false; //子目錄變量(是否在緩存文件夾中生成子目錄)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';4 在配置文件autoload.php自動加載ci_smarty $autoload['libraries']=array('ci_smarty');5 在框架的擴展父類MY_Controller.php(沒有就現在core下創建)添加如下代碼 / * @param $key * @par * smarty assign */
public function assign($key,$val){
$this->cismarty->assign($key,$val);
}
/**
* @param $html
* smarty smarty display方法
*/
public function display($html,$is_cache=false){
if(!$is_cache)
{
$this->ci_smarty->clearCache($html);
}
$this->ci_smarty->display($html);}
/**
* smarty清除所有緩存
* @author shangshikai
*/
public function clearAllCache(){
$this->ci_smarty->clearAllCache();
}
/**
* smarty 清除某個模板的緩存
* @author shangshikai
*/
public function clearCache($html){
$this->ci_smarty->clearCache($html);
}/**
* @param $html
* @return mixed
* smarty判斷該模板是否有緩存
*/
public function isCached($html)
{
return $this->ci_smarty->isCached($html);
}6 由于在配置文件smarty.php中開啟緩存,但不是所有頁面都適合緩存,所以在MY_Controller中配置display方法時應增加參數默認清除緩存,需要緩存的頁面只需在調用display方法時傳遞第二個參數為true。在使用緩存后,如果需要局部不需要緩存可以使用{nocache}{/nocache}標簽包裹,如果標簽不緩存使用方法是在標簽后增加nocache 如{foreach $arr as $v nocache} 7 如果整個項目都不使用緩存,可以在smarty.php中去掉$config['cache_lifetime'] = 3600;$config['caching'] = true;兩行,并且在MY_Controller中的display方法去掉第二個參數以及相關判斷 以上就是CI框架下smarty3的整合步驟(附代碼)的詳細內容,更多請關注php中文網其它相關文章! 網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。 |
溫馨提示:喜歡本站的話,請收藏一下本站!