CodeIgniter3でSmartyを使う。
CodeIgniter 3.1.6
Smarty 3.1.30
・CodeIgniter
The small framework with powerful features
・Smarty
1.CodeIgniterを展開(インストールはZip解凍を想定)
2.Smartyを解凍して、中にあるlibフォルダ内の全部をCodeIgniterの「system/libraries」に「Smarty」フォルダを作ってコピペする。
※ Smarty 3.1.30 だとlibの中身
フォルダ構成
3.Smartyフォルダ内に「Smarty.php」を作成
適当にSmartyの設定を書き込む
<?php if (!defined('BASEPATH')) exit('Smarty.php Error'); require_once(BASEPATH.'libraries/Smarty/Smarty.class.php'); class CI_Smarty extends Smarty { public function __construct() { parent::__construct(); $this->template_dir = APPPATH . 'views/templates'; $this->compile_dir = APPPATH . 'views/templates_c'; $this->left_delimiter = '{{'; $this->right_delimiter = '}}'; $this->loadFilter('variable', 'htmlspecialchars'); } }
4.Smarty用のフォルダを「application/views」の下にtemplatesとtemplates_cフォルダ作成
templates_cフォルダはパーミッションを777とかにしておく
5.CodeIgniterのAutoloadに設定
application/config/autoload.php
$autoload['libraries'] = array('smarty');
6.Smartyを使う
適当なControllerに書く
public function index() { $this->smarty->assign('message', 'test'); $this->smarty->display("index.tpl"); }
適当にtemplateを作る(上に合わせてindex.tplにして1行だけ)
template/index.tpl
{{message}}
※「{{」「}}」になってるのはSmarty.phpで指定しています