LoginSignup
10
10

More than 5 years have passed since last update.

hamlからslimへの移行

Last updated at Posted at 2014-10-09

hamlよりslimの方がパフォーマンスが良いとのことなので移行することにしました。
移行にはhaml2slimを使用します。

haml2slim
https://github.com/slim-template/haml2slim

始める前に

もちろんですが、実施する前にはbranchを切るなどしていつでも戻せるようにしておきましょう。

あばよhaml,よろしくslim

とりあえずGemfileを編集しましょう。

Gemfile
- gem 'haml-rails'
+ gem 'slim-rails'
+ gem 'haml2slim', :group => :development

とりあえず-hでヘルプを見ます。

$ bundle exec haml2slim -h

Usage: haml2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
        --trace                      Show a full traceback on error
    -d, --delete                     Delete HAML files
    -h, --help                       Show this message
    -v, --version                    Print version

それでは実行しましょう。バシッといきます。

$ bundle exec haml2slim .

この時点でgit statusを見ると、hamlファイルがあったところにslimファイルが生成されているかと思います。

hamlとはもうおさらばするので、こちらもバシッと消してしまいます。

$ bundle exec haml2slim -d .

再度git statusを確認すると、hamlファイルが削除されているかと思います。

このタイミングで一度railsサーバを立ち上げて動作確認してみましょう。

が・・・・駄目っ・・・・・!

動作確認を行っていたところ、以下のようなエラーが発生しました。

Slim::Parser::SyntaxError - Text line not indented deep enough.
The first text line defines the necessary text indentation.
Are you trying to nest a child tag in a tag containing text? Use | for the text block!

エラー箇所はapplication.html.slimの「head」となっていました。その時の状態は以下です。

application.html.slim
doctype html
html lang: "ja"
  head
...

slimではhamlのようにRuby1.9のHashシンタックスが使えないようで、以下のように書きなおす必要があります。

application.html.slim
doctype html
- html lang: "ja"
+ html lang="ja"
  head
...

こちらの問題についてはPRが出ているみたいですがまだマージはされていないようです。
https://github.com/slim-template/haml2slim/pull/23

ちまちまと直し...

同じような箇所が多々あったので、1つずつ直していったところ無事動きました。
現在hamlを使っていて、slimに移行したいという方の参考になれば幸いです。

Special Thanks

hamlからslimへの移行でひっかかったこと - @znz blog
http://blog.n-z.jp/blog/2014-03-11-haml-to-slim.html

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