ホームページ  >  記事  >  バックエンド開発  >  CI フレームワークでの Smarty3 の統合手順 (コード付き)

CI フレームワークでの Smarty3 の統合手順 (コード付き)

不言
不言転載
2018-10-08 14:23:241593ブラウズ

この記事の内容は、smarty3 を CI フレームワークに統合する手順 (コード付き) に関するもので、一定の参考価値があります。必要な友人は参考にしていただければ幸いです。

1 Smarty3 をダウンロードし、libs ファイルをフレームワーク ライブラリ ディレクトリに配置し、名前を Smarty に変更します。
2 ライブラリの下に Ci_smarty.php ファイルを作成します。コードは次のとおりです。

<?php  
if ( ! defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);

require_once(APPPATH.&#39;libraries/smarty/Smarty.class.php&#39;);     //这里指定Smarty.class.php的存放位置
class Ci_smarty extends Smarty
{
    protected $ci;
    public function __construct()
    {
        parent::__construct();
        $this->ci = & get_instance();
        $this->ci->load->config(&#39;smarty&#39;);//加载smarty的配置文件
        $this->cache_lifetime =$this->ci->config->item(&#39;cache_lifetime&#39;);
        $this->caching = $this->ci->config->item(&#39;caching&#39;);
        $this->config_dir = $this->ci->config->item(&#39;config_dir&#39;);
        $this->template_dir = $this->ci->config->item(&#39;template_dir&#39;);
        $this->compile_dir = $this->ci->config->item(&#39;compile_dir&#39;);
        $this->cache_dir = $this->ci->config->item(&#39;cache_dir&#39;);
        $this->use_sub_dirs = $this->ci->config->item(&#39;use_sub_dirs&#39;);
        $this->left_delimiter = $this->ci->config->item(&#39;left_delimiter&#39;);
        $this->right_delimiter = $this->ci->config->item(&#39;right_delimiter&#39;);
    }
}

3 「フレームワーク構成ディレクトリにsmarty.phpを作成する」のコードは次のとおりです。

<?php
$config[&#39;cache_lifetime&#39;] = 3600;//缓存失效
$config[&#39;caching&#39;] = true;//开启缓存
$config[&#39;template_dir&#39;] = APPPATH .&#39;views&#39;;
$config[&#39;compile_dir&#39;] = APPPATH .&#39;views/template_c&#39;;
$config[&#39;cache_dir&#39;] = APPPATH . &#39;views/cache&#39;;
$config[&#39;config_dir&#39;] = APPPATH . &#39;views/config&#39;;
$config[&#39;use_sub_dirs&#39;] = false; //子目录变量(是否在缓存文件夹中生成子目录)
$config[&#39;left_delimiter&#39;] = &#39;{&#39;;
$config[&#39;right_delimiter&#39;] = &#39;}&#39;;

4 拡張親の構成ファイルautoload.phpにci_smarty

$autoload[&#39;libraries&#39;]=array(&#39;ci_smarty&#39;);

5を自動的にロードします。フレームワークのクラス MY_Controller.php (そうでない場合は、コアだけを作成します) 次のコードを追加します

/ * @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 で表示メソッドを構成するときは、デフォルトでキャッシュをクリアするためのパラメータを追加する必要があります。これにはキャッシュが必要です。ページでは、表示メソッドを呼び出すときに 2 番目のパラメータを true として渡すだけで済みます。キャッシュを使用した後、ローカルにキャッシュする必要がない場合は、{nocache}{/nocache} タグ パッケージを使用できます。タグがキャッシュされていない場合は、{foreach $ のようにタグの後に nocache を追加する方法です。 arr as $v nocache}

7 プロジェクト全体でキャッシュを使用しない場合は、smarty の $config['cache_lifetime'] = 3600; $config['caching'] = true; の 2 行を削除できます。 .php を作成し、MY_Controller の表示メソッドの 2 行目を削除します。パラメータと関連する判断

以上がCI フレームワークでの Smarty3 の統合手順 (コード付き)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はcnblogs.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。