LoginSignup
9
10

More than 5 years have passed since last update.

FuelPHP1.7.3のテンプレートエンジンにsmartyを設定

Last updated at Posted at 2015-07-02

FuelPHP1.7.3でsmarty3系を使えるようにした。

1.composer.jsonの修正

requireの箇所にsmartyの記述を追記する。

composer.json
"require": {
        "php": ">=5.3.3",
        "composer/installers": "~1.0",
        "fuel/docs": "dev-1.7/master",
        "fuel/core": "dev-1.7/master",
        "fuel/auth": "dev-1.7/master",
        "fuel/email": "dev-1.7/master",
        "fuel/oil": "dev-1.7/master",
        "fuel/orm": "dev-1.7/master",
        "fuel/parser": "dev-1.7/master",
        "fuelphp/upload": "2.0.2",
        "monolog/monolog": "1.5.*",
        "michelf/php-markdown": "1.4.0",
        "smarty/smarty": "3.*"
    },

2.composerの依存ライブラリをupdate

composer.jsonの修正を反映させる。

$ php composer.phar update

3.Parserパッケージを有効にする

fuel/app/config/config.phpを修正する。(always_loadの箇所)

config.php
    /**************************************************************************/
    /* Always Load                                                            */
    /**************************************************************************/
    'always_load'  => array(

        /**
         * These packages are loaded on Fuel's startup.
         * You can specify them in the following manner:
         *
         * array('auth'); // This will assume the packages are in PKGPATH
         *
         * // Use this format to specify the path to the package explicitly
         * array(
         *     array('auth' => PKGPATH.'auth/')
         * );
         */
         'packages'  => array(
            //'orm',
            'parser',
         ),

4.parser.phpの修正

fuel/packages/parser/config/parser.phpを修正する。
View_SmartyincludeのKEYを追記する。

parser.php
// SMARTY ( http://www.smarty.net/documentation )
    // ------------------------------------------------------------------------
    'View_Smarty' => array(
        'include' => APPPATH.'vendor'.DS.'smarty'.DS.'libs'.DS.'Smarty.class.php',
        'auto_encode' => true,
        'delimiters'  => array('left' => '{', 'right' => '}'),
        'environment' => array(
            'compile_dir'       => APPPATH.'tmp'.DS.'Smarty'.DS.'templates_c'.DS,
            'config_dir'        => APPPATH.'tmp'.DS.'Smarty'.DS.'configs'.DS,
            'cache_dir'         => APPPATH.'cache'.DS.'Smarty'.DS,
            'plugins_dir'       => array(),
            'caching'           => false,
            'cache_lifetime'    => 0,
            'force_compile'     => false,
            'compile_check'     => true,
            'debugging'         => false,
            'autoload_filters'  => array(),
            'default_modifiers' => array(),
        ),
    ),

5.templates_cなどのフォルダ作成

smartyでコンパイル後のファイルを格納するフォルダなど3つ作成。
3つともパーミッションは777に。

fuel/app/tmp/Smarty/configs
fuel/app/tmp/Smarty/templates_c
fuel/app/cache/Smarty

6.コントローラからビューの呼び出し例

controller

fuel/app/classes/controller/welcome.php
/**
     * The test action for the application.
     *
     * @access  public
     * @return  Response
     */
    public function action_test()
    {
        $t_params = array(
            'message' => 'smarty template テスト'
        );
        $view = View::forge('welcome/test.html');
        $view->title = 'テスト!';
        $view->username = '太郎';
        return $view;
    }

template

fuel/app/views/welcome/test.html
test.html
{$title}{$username}

7.表示

Compile Error!

エラーです。
以下の対応をしたら直りました。

view.phpの修正

fuel/packages/parser/classes/view.phpを修正する。
正しい対応かはわからないが、38行目,89行目のrequire $include;をコメントアウトする。
これで動くようになった。

参考

FuelPHP 1.7でのSmartyのセットアップ
Fuelphp : class ‘smarty’ not found
FuelPHPのコントローラからビューへ変数を渡す

9
10
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
9
10