新しくアプリを作ることになったのでKohanaフレームワークを試用中。
以前CakePHPを利用したときにDBと密過ぎて違和感を感じたのでもっと手軽感のあるフレームワークを探したらいきつたい。
最初はCodeIgniter(以降CI)をちょっと試してみて良さそうだったのだけどこちらの記事を見て止めた。
セッションデータをクッキーに入れるのはなんか嫌だ。
で、CIを調べていたときにKohanaフレームワークがある事を知っていたので調査してみる。
CIから派生しただけあってデフォルトではクッキーに保存するのだけれど
configの設定でdriverを’native’にすると通常のセッションが利用できそうだったのでKohanaに決めた。
Kohanaにチャレンジして早速問題が。テンプレートエンジンが無い!
CIも同じなんだけどKohanaもテンプレートにPHPタグ(<?php ~ ?>)を書いて変数にアクセスする。
速度面で有利かも知れないけどやっぱりテンプレートエンジンは利用したい。
Smarty利用しようかとも思ったのだけれどそろそろ飽きたし調査ついでに新しいものに手をだすことにした。
それでPHPTAL
KohanaとPHPTALを連携するライブラリを書いたので晒します。
前提
・Kohanaがセットアップされている
・PHPTALがセットアップされている
■設定ファイルとライブラリ
/system/application/config/ptal.php
<?php defined('SYSPATH') or die('No direct access allowed.'); /* * File: Ptal * * Options: * path - * suffix - */ $config = array ( 'dir' => APPPATH.'views/', 'suffix' => '.html' );
/system/libraries/Ptal.php(とりあえずレベルって事で^^;)
<?php if (!defined('SYSPATH')) exit('No direct script access.'); /* * Class: Ptal * * author - t@setunai.net * license - */ require "PHPTAL.php"; class Ptal_Core { protected static $config; /* * Constructor: __construct * */ public function __construct() { // Load config self::$config = Config::item('ptal'); } /* * Method: fetch * * Parameters: * tmpl - * data - * * Returns: * fetch tmpl */ function fetch($tmpl, $data) { $dir = self::$config['dir']; $suffix = self::$config['suffix']; $path = sprintf("%s%s%s", $dir, $tmpl, $suffix); $phptal = new PHPTAL($path); foreach($data as $p => $value) { $phptal->$p = $value; } $result = ""; try { $result = $phptal->execute(); } catch (Kohana_Exception $e){ throw new Kohana_Exception('phptal error'); } return $result; } /* * Method: fetch * * Parameters: * tmpl - * data - * */ function view($tmpl, $data) { echo $this->fetch($tmpl, $data); } } // End Ptal Class
■サンプル
コントローラ:/application/controllers/test.php
<?php defined('SYSPATH') or die('No direct script access.'); class Test_Controller extends Controller { function index() { $days["sun"] = "日曜日"; $days["mon"] = "月曜日"; $days["tue"] = "火曜日"; $days["wed"] = "水曜日"; $data["title"] = "PHPTALテスト"; $data["days"] = $days; $data["bol"] = false; $this->load->library("Ptal"); $this->Ptal->view("test/index", $data); } }
テンプレート:/application/views/test/index.html
<html> <head> <title tal:content="title">タイトル</title> </head> <body> <table> <tr> <th>キー</th> <th>値</th> <th>index</th> <th>number</th> </tr> <tr tal:repeat="day days"> <td tal:content="repeat/day/key">英字</td> <td tal:content="day">曜日</td> <td tal:content="repeat/day/index" /> <td tal:content="repeat/day/number" /> </tr> </table> <p tal:condition="bol">bolがtrueなら表示</p> <p tal:condition="not:bol">bolがfalseなら表示</p> </body> </html>
実行結果のソース
<html> <head> <title>PHPTALテスト</title> </head> <body> <table> <tr> <th>キー</th> <th>値</th> <th>index</th> <th>number</th> </tr> <tr> <td>sun</td> <td>日曜日</td> <td>0</td> <td>1</td> </tr><tr> <td>mon</td> <td>月曜日</td> <td>1</td> <td>2</td> </tr><tr> <td>tue</td> <td>火曜日</td> <td>2</td> <td>3</td> </tr><tr> <td>wed</td> <td>水曜日</td> <td>3</td> <td>4</td> </tr> </table> <p>bolがfalseなら表示</p></body> </html>
PHPTAL初めて使ったけど良いね。
テンプレートがすごく綺麗。
Kohana、PHPTALどちらも日本語情報少ないね。それが心配^^;
—-
参考URL
Kohana
http://kohanaphp.com/home.html
PHPTAL
http://phptal.motion-twin.com/
CodeIgniter(日本CodeIgniterユーザ会)
http://codeigniter.jp/
CodeIgniterのセッションは評判悪いですね。
それを受けて反映させたのか今のバージョンは
セッションをデフォルトでDBで管理できるようになったようです。