Codeigniter3にSmartyをComposerで設定

Codeigniter3を使ってみました。そこでテンプレートエンジンも設定することになり、今回はSmartyを選択し、Composerで設定までしたので、その手順を記載します。

気になっている別のテンプレートエンジンtwigについては、夏にトライしてみようと思います。

動作環境

  • Codeigniter3
  • PHP7.0.3
  • CentOS6.5

Composerのインストール

今回はapplicationディレクトリの1つ上の階層(Codeigniterのルートディレクトリ)で、

$ curl -sS https://getcomposer.org/installer | php

Smartyのインストール

applicationディレクトリに移動して

$ cd application

Smartyのインストール

$  php ../composer.phar require smarty/smarty

そうするとapplication/composer.jsonが生成されます。
内容はこんな

{
    "require": {
        "smarty/smarty": "^3.1"
    }
}

そして、Smarty本体はapplication/vendor/smartyです。

.gitignoreの編集

今回はapplication/vendor/以下は、git管理対象外とし、composer.jsonのみを管理し各々の環境においては、application直下にてcomposer installまたは(今回の場合)php ../composer.phar installで必要なライブラリをインストールするようにします。

.gitignore
以下の内容を追記しておきます。

application/vendor/*

ディレクトリの作成

Smarty用のテンプレートディレクトリとテンプレートキャッシュ用のディレクトリを作成します。

application/views/templates
application/views/templates_c

の2つのディレクトリをchmod 777で作成しておきます。

Smarty用のライブラリ作成

さきほど作成したディレクトリをSmarty側に認識させるため、Codeigniterでライブラリを1つ作ります。
system/libraries/Smarty.php

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

class CI_Smarty extends Smarty {

    public function __construct()
    {
        parent::__construct();

        $this->compile_dir = APPPATH . "views/templates_c";
        $this->template_dir = APPPATH . "views/templates";
        $this->assign( 'APPPATH', APPPATH );
        $this->assign( 'BASEPATH', BASEPATH );

        // Assign CodeIgniter object by reference to CI
        if ( method_exists( $this, 'assignByRef') )
        {
            $ci =& get_instance();
            $this->assignByRef("ci", $ci);
        }
        log_message('debug', "Smarty Class Initialized");
    }

    function view($template, $data = array(), $return = FALSE)
    {
        foreach ($data as $key => $val)
        {
            $this->assign($key, $val);
        }

        if ($return == FALSE)
        {
            $CI =& get_instance();
            if (method_exists( $CI->output, 'set_output' ))
            {
                $CI->output->set_output( $this->fetch($template) );
            }
            else
            {
                $CI->output->final_output = $this->fetch($template);
            }
            return;
        }
        else
        {
            return $this->fetch($template);
        }
    }
}

Codeigniter側の設定

autoloadの追加

先ほど作成したライブラリを読み込ませます。
application/config/autoload.php

$autoload['libraries'] = array('smarty');

composerのautoload

application/config/config.php

$config['composer_autoload'] = TRUE;

これでapplication/vendor/autoload.phpが読み込まれるようになります。

試してみる

今回はひとまずsmartyのtemplate呼び出しまで。

controller

  • application/controllers/hoge.php
class Hoge extends CI_Controller {

    public function index(){
        $this->smarty->view('hoge_index.html');
    }
}

view

  • application/views/templates/hoge_index.html
hoge template

以上で、Codeigniter3とSmarty3をcomposerで連携できた。

参考サイト:
CodeIgniter 3 とSmarty連携させる

[Sponsored Links]







  • このエントリーをはてなブックマークに追加
[Sponsored Links]