このエントリーをはてなブックマークに追加

更新日: 2016年2月25日

実行時間: 0.0069

Smarty連携

 CodeIgniterとSmartyを連携する方法はそれほど難しくはありません。
 大まかなインストールの流れは以下のようになります。

  1. Smartyのダウンロード
  2. application/third_paty/ ディレクトリに設置
  3. ディレクトリ名、ファイル名の変更
  4. テンプレートファイルを設置するディレクトリの作成
  5. autoload.php の設定 (必要があれば)

 インストールしたSmartyの利用方法には2通りあります。

  1. CI_Controllerを拡張(オーバーライド)して利用する方法
  2. コントローラ毎に利用する方法
(1.)の方法は、全てのコントローラで共通してSmartyを利用する場合に便利です。
(2.)の方法は、一部のコントローラのみでSmartyを利用する場合に選択することになると思います。

Smartyのインストール

 まず、PHPのバージョンを確認しましょう。PHPのバージョンが「5.2」以上であれば「Smarty 3.x」系が利用できます。 PHPのバージョンが「5.1」以下の場合は、「Smarty 2.x」系をダウンロードしてください。

  • Smartyダウンロード
  • cd /tmp
    wget http://www.smarty.net/files/Smarty-3.1.8.tar.gz
  • Smarty解凍・展開
  • tar zxvf Smarty-3.1.8.tar.gz -C APP_ROOT/application/third_party/

    「application/third_paty/」ディレクトリに展開します。

  • ディレクトリ名・ファイル名の変更
  • cd APP_ROOT/application/third_party/
    mv Smarty-3.1.8 smarty
    mv smarty/libs smarty/libraries
    mv smarty/libraries/Smarty.class.php smarty/libraries/Smarty.php

    「Smarty-3.1.8」ディレクトリ名を「smarty」に変更します。
    「libs」ディレクトリ名を「libraries」に変更します。
    「Smarty.class.php」ファイル名を「Smarty.php」に変更します。

  • テンプレートディレクトリ作成
  • mkdir application/views/templates
    mkdir application/views/templates_c

    「templates_c」ディレクトリのパーミッションを適宜変更してください。

  • autoload.phpの設定
  • $autoload['packages'] = array(APPPATH . 'third_party/smarty');
    $autoload['libraries'] = array('smarty');

    一部のコントローラでのみSmartyを利用する場合は、 「autoload.php」での自動読込設定は必要ないかもしれません。適宜、設定してください。

Smartyの利用

 Smartyを利用するには、 CI_ControllerクラスをオーバーライドしてSmartyを利用する方法、 コントローラ毎に必要に応じてSmartyを利用する方法があります。

CI_Controllerクラスの拡張

    autoload.phpで自動読込の設定がされていることを前提に説明していきます。

    • MY_Controller.php
    •                             
      <?php
      class My_Controller extends CI_Controller {
          protected $template;
      
          public function __construct()
          {
              parent::__construct();
              $this->smarty->template_dir = APPPATH . 'views/templates';
              $this->smarty->compile_dir  = APPPATH . 'views/templates_c';
              $this->template = 'layout.tpl';
          }
      
          public function view($template)
          {
              $this->template = $template;
          }
      
          public function _output($output)
          {
              if (strlen($output) > 0) {
                  echo $output;
              } else {
                  $this->smarty->display($this->template); 
              }
          }
      }
                                  
                                  

      「application/core/」ディレクトリにCI_Controllerの拡張ファイルを作成します。

    • サンプルコントローラ(application/controllers/sapmle.php)
    •                             
      <?php
      class Sample extends MY_Controller {
          public function __construct()
          {
              parent::__construct();
          }
      
          public function my_action()
          {
              $this->view('my_action.tpl');
          }
      }
                                  
                                  

      拡張したコントローラを更にオーバーライドしてコントローラクラスを作成しています。

コントローラ毎で利用

     一部のコントローラでのみSmartyを利用する場合などは、 自動ロード設定はせずに各コントローラで初期化した方が Webアプリケーション全体のパフォーマンスが良くなります。

    • サンプルコントローラ(application/controllers/sample.php)
    •                             
      <?php
      class Sample extends MY_Controller {
          public function __construct()
          {
              parent::__construct();
              $this->load->library('smarty');
              $this->smarty->template_dir = APPPATH . 'views/templates';
              $this->smarty->compile_dir  = APPPATH . 'views/templates_c';
          }
      
          public function my_action()
          {
              $this->smarty->assign('data', 'any data'); 
              $this->smarty->display('my_action.tpl'); 
          }
      }