既存のerbをhamlに一括変換する
erb2hamlは不要
erbファイルをhamlに変換する際に、erb2haml
がよく参照されていますが、実はhaml-rails
だけで十分です。
以下は、erb2hamlを使う方法をまとめていますが、haml-railsもインストールしており、デフォルトをHamlにしたいのなら、erb2hamlは不要です。
haml-railsで後から一括変換
コマンドは以下のとおり。ちなみに、Rails5.0からはrakeコマンドをrailsコマンドに統一する流れとなってます。
rails haml:erb2haml
一気にerbファイルも削除したい場合は、
HAML_RAILS_DELETE_ERB=true rails haml:erb2haml
試しに、rails g devise:views
で生成されるDeviseのerbファイルがある状態でhamlに変換してみます。
$ bundle exec rails haml:erb2haml
--------------------------------------------------------------------------------
Generating HAML for app/views/devise/unlocks/new.html.erb...
Generating HAML for app/views/devise/passwords/edit.html.erb...
Generating HAML for app/views/devise/passwords/new.html.erb...
Generating HAML for app/views/devise/mailer/confirmation_instructions.html.erb...
Generating HAML for app/views/devise/mailer/reset_password_instructions.html.erb...
Generating HAML for app/views/devise/mailer/password_change.html.erb...
Generating HAML for app/views/devise/mailer/email_changed.html.erb...
Generating HAML for app/views/devise/mailer/unlock_instructions.html.erb...
Generating HAML for app/views/devise/shared/_links.html.erb...
Generating HAML for app/views/devise/sessions/new.html.erb...
Generating HAML for app/views/devise/confirmations/new.html.erb...
Generating HAML for app/views/devise/registrations/edit.html.erb...
Generating HAML for app/views/devise/registrations/new.html.erb...
Generating HAML for app/views/layouts/mailer.html.erb...
Generating HAML for app/views/layouts/mailer.text.erb...
--------------------------------------------------------------------------------
HAML generated for the following files:
app/views/devise/unlocks/new.html.erb
app/views/devise/passwords/edit.html.erb
app/views/devise/passwords/new.html.erb
app/views/devise/mailer/confirmation_instructions.html.erb
app/views/devise/mailer/reset_password_instructions.html.erb
app/views/devise/mailer/password_change.html.erb
app/views/devise/mailer/email_changed.html.erb
app/views/devise/mailer/unlock_instructions.html.erb
app/views/devise/shared/_links.html.erb
app/views/devise/sessions/new.html.erb
app/views/devise/confirmations/new.html.erb
app/views/devise/registrations/edit.html.erb
app/views/devise/registrations/new.html.erb
app/views/layouts/mailer.html.erb
app/views/layouts/mailer.text.erb
--------------------------------------------------------------------------------
Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)
y
Deleting original .erb files.
--------------------------------------------------------------------------------
Task complete!
No .erb files found. Task will now exit.
ご覧のとおり、erb2haml無しで変換できました。
その他の使い方
haml-rails
gem自体は、RailsでHaml形式をサポートするライブラリですが、他にもデフォルトテンプレートエンジンをERBからHamlに変更する機能もあります。
haml-rails
の使い方を以下にまとめておきます。
1. インストール
gemfileにhaml-rails
を追記して、bundle install
しましょう。
これによって、contoroller、scaffold、mailerを生成する際はerbではなく、hamlが生成されます。
こんな感じで。
$ bundle exec rails g controller welcome index
Running via Spring preloader in process 40826
create app/controllers/welcome_controller.rb
route get 'welcome/index'
invoke haml ←ここ!!
create app/views/welcome
create app/views/welcome/index.html.haml ←ここ!!
invoke rspec
create spec/controllers/welcome_controller_spec.rb
exist spec/views/welcome
identical spec/views/welcome/index.html.haml_spec.rb
基本的にはこれだけでOKですが、続けて以下も実行しましょう。
2. application.html.erbを変換
以下を実行して、アプリケーションレイアウトをhamlに変換しましょう。その後、application.html.erbを削除します。
こうすることで、Railsがapplication.html.erbではなく、application.html.hamlを使用するようになります。
$ bundle exec rails g haml:application_layout convert
Running via Spring preloader in process 9113
Success! app/views/layouts/application.html.haml is created.
Please remove the erb file: app/views/layouts/application.html.erb
ちなみに、haml-rails
だけで十分と言っていますが、前提gemとしてhtml2haml
が自動でインストールされており、上の機能は実際のところhtml2haml
を使用しています。
3. 他の既存のerbをhamlに変換
基本的にコントローラ等を生成するとデフォルトでhamlになりますが、冒頭のdevise等はerbで出力される(参考:DeviseのビューをHamlとしてカスタマイズする方法)ので、必要に応じてそれらをhamlに変換します。
既に一度hamlに変換していて、erbが削除されていない場合は、以下のようなメッセージが出力されて上書きするかどうか聞いてきます。
Would you like to overwrite these .haml files? (y/n)
ちなみに、こっちも裏ではhtml2haml
が動いています。
参考
備忘録
テンプレートエンジンについては、Railsガイドを参照。
と言っても自分もまだ初心者なので勉強しないと。。。