LoginSignup
1
0

More than 5 years have passed since last update.

FuelPHP で Parser ライブラリを使いつつ、ViewModel を使う

Last updated at Posted at 2012-02-16

ViewModel の場合、読み込むテンプレートの指定が少し特殊だったのでメモ
テストとして言語ファイルを読み込んで出力してみる。

まず、設定に parser を追加

app/config/config.php
<?php
// ..
    'always_load' = array(
        'packages' => array(
            'parser',
        )
    );
// ..

読む込む言語ファイルはこちら

app/lang/ja/test.php
<?php

return array(
    'label' => array(
        'hoge' => 'Hoge!',
        'fuga' => 'Fuga!',
    );
);

出力したいテンプレートはこちら

app/views/welcome/index.html.twig
{{ labels.hoge }}
{{ labels['fuga'] }}

コントローラでの ViewModel の使い方はいつも通り。

app/classes/controller/welcome.php
<?php

class Controller_Welcome extends Controller
{
    public function before()
    {
        \Lang::load('test');
    }

    public function action_index()
    {
        return \Response::forge(\ViewModel::forge('welcome/index'));
    }
}

ViewModel はこちら

app/classes/model/welcome/index.php
<?php

class View_Welcome_Index extends \ViewModel
{
    protected $_view = 'welcome/index.html.twig';

    public function view()
    {
        $this->labels = array(
            'hoge' => __('label.hoge'),
            'fuga' => __('label.fuga')
        );
    }
}

しかし使い辛い。。
もっといい方法があるのかな〜

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0