FuelPHP で haml-to-php を使う

正しい修正かわからないけどとりあえず作業メモ.

まずは fuel/app/vendor 以下に haml-to-php を取得(他のパッケージは git の submodule で管理されてるっぽいけどよくわかってないのでとりあえず git clone )

$ git clone git://github.com/MarcWeber/haml-to-php.git

parser パッケージを用いるように設定.

fuel/app/config/config.php
<?php

return array(
  'always_load' => array(
    'packages' => array(
      'parser',
    )
  ),
);

読み込むファイルを上書き設定.

fuel/app/config/parser.php
<?php

return array(
       'View_Haml'   => array(
               'include'      => APPPATH.'vendor'.DS.'haml-to-php'.DS.'haml'.DS.'Haml.php',
       ),
);

packages のなかにある parser の方を修正

diff --git a/fuel/packages/parser/classes/view/haml.php b/fuel/packages/parser/classes/view/haml.php
index efd4877..954d75e 100644
--- a/fuel/packages/parser/classes/view/haml.php
+++ b/fuel/packages/parser/classes/view/haml.php
@@ -14,7 +14,7 @@
 
 namespace Parser;
 
-use HamlParser;
+use HamlFileCache;
 
 class View_Haml extends \View
 {
@@ -27,9 +27,7 @@ class View_Haml extends \View
                $file = $file_override ?: $this->file_name;
 
                static::cache_init($file);
-               $file = static::parser()->parse($file, static::$_cache);
-
-               return parent::process_file($file);
+               return static::parser()->haml($file, $this->data);
        }
 
        public $extension = 'haml';
@@ -46,7 +44,7 @@ class View_Haml extends \View
                        return static::$_parser;
                }
 
-               static::$_parser = new HamlParser();
+               static::$_parser = new HamlFileCache(null, static::$_cache);
 
                return static::$_parser;
        }

後は haml ファイルを作成して下記の用な感じで haml ファイルを読み込めばおk.

$this->template->content = View::forge('hoge/index.haml', array());

ただし,php54 で使ってると notice が発生する可能性があるので下記のように変更した.例えば クラスを複数してしたときなど.(具体的には haml-to-php のテストを動かせばわかる)

diff --git a/haml/HamlParser.php b/haml/HamlParser.php
index ac0f3f5..af96b2b 100644
--- a/haml/HamlParser.php
+++ b/haml/HamlParser.php
@@ -659,7 +659,7 @@ protected function flattenThing(array $thing, $parent_tag = ''){
             }
           }
           // render classes (dynamic because haml removes duplicates. The duplicates are known at runtime)
-          array_unique($class);
+          array_unique($class, SORT_REGULAR);
           if (count($class) > 0){
             $this->rText(" class=$q", false);
             $class_items = array();