LoginSignup
2

More than 5 years have passed since last update.

FuelPHP1.7.3にsmartyと設定を追加する

Last updated at Posted at 2017-08-06

概要

FuelPHPのビューでSmartyを使う要件があったので、設置したメモを残します。

ComposerでSmartyを追加する

まず、composer.jsonに「"smarty/smarty": "3.*"」を追加します。

composer.json
"require": {
    "php": ">=5.3.3",
    "composer/installers": "~1.0",
    "fuel/core": "dev-1.7/master",
                 
                 
                 
    "smarty/smarty": "3.*"
    },

composer.jsonの変更を反映させる。

$ php composer.phar self-update
$ php composer.phar update

※ composer.lockファイルも更新されるので、gitに一緒にコミットしましょう。

Parserパッケージ有効化

FuelPHPのconfig.phpを修正する。

fuel/app/config/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'
     ),

parser.phpの作成

「fuel/packages/parser/config/parser.php」を修正するのが手っ取り早いですが、
composerで管理しているファイルなので修正NGとなります。

他の方法を探したところ、下記のファイルを作成すると設定を上書きできるようです。

下記オリジナル設定を追加する

  • 拡張子を「tpl」に変更する
  • デリミタを「{%, %}」に変更する
fuel/app/config/parser.php
<?php

return array(
    // 拡張の定義
    'extensions' => array(
        'tpl'    => 'View_Smarty',
    ),
    // Smarty設定 ( http://www.smarty.net/documentation )
    'View_Smarty' => array(
        '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()
        )
    )
);

各種tmpフォルダを作成する

Smartyフォルダとさらにその下層にconfigs, templates_cフォルダを作成する必要があります。

$ mkdir -p fuel/app/tmp/Smarty/configs/
$ mkdir -p fuel/app/tmp/Smarty/templates_c/

.gitignoreの除外条件を調整する。

configs, templates_cフォルダは空フォルダとして必要なので、各フォルダ内に「.gitkeep」ファイルを追加します。

$ touch fuel/app/tmp/Smarty/configs/.gitkeep
$ touch fuel/app/tmp/Smarty/templates_c/.gitkeep

ただし、実際作成される一時ファイルは、gitのコミット対象外としたいので、そのように条件を追加する。

.gitignore
/fuel/app/tmp/*
!/fuel/app/tmp/Smarty/configs/.gitkeep
!/fuel/app/tmp/Smarty/templates_c/.gitkeep

これで、fuel/app/tmp/フォルダがSmartyフォルダは除外してgit管理対象外となります。

コントローラを作ってみる

fuel/app/classes/controller/smarty.php
<?php

/**
 * Smartyサンプル
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Smarty extends Controller
{
    /**
     * Smartyテンプレートのテスト
     *
     * @access  public
     * @return  Response
     */
    public function action_index()
    {
        return View_Smarty::forge('smarty/index.tpl', array('name' => 'hoge'));

}

ビューを作成する

テストするため、viewsフォルダ内にテンプレートを作成してみる。

fuel/app/views/smarty/index.tpl
<html>
<body>
{%$name%}
</body>
</html>

テストしてみる

無事に表示されました。

www.example.com
hoge

以上です。

参考サイト

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
2