メメメモモ

プログラミング、筋トレ、ゲーム、etc

Controller_TemplateとViewModelを使った場合

テンプレートエンジンにPHPを使う場合は、Controllerの代わりにController_Templateを継承するようにしたほうが良さそう。
Controller_Templateを継承すると、ベースとなるテンプレートを読み込むようになる。
デフォルトでは「views/template.php」をベーステンプレートとして読み込む。
ベーステンプレートの内容は、次のようなものになる。

<!DOCTYPE html>
<html>
   <head>
       <title><?= $title ?></title>
   </head>
   <body>
       <?= $content ?>
   </body>
</html>


このテンプレート内で使われている変数に値をセットするときは、コントローラで以下のように書く。

class Controller_Index extends Controller_Template
{
    public function action_list()
    {
        $this->template->title = 'List Page';
        $this->template->content = 'Content';
    }
}

「$this->template->content」にページの内容をセットするようにする。
contentにセットする内容は、ViewModelで作成したほうが良さそう。
次のような感じになるだろう。

$this->template->content = ViewModel::forge('index/list');

ViewModelは「fuel/app/classes/view」に作成する。
ViewModelについては以下のリンクを参照。