Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Haml to work with Rails?

People also ask

Should I use Haml or ERB?

ERB has a simple syntax for mixing HTML and Ruby commands, but it can be somewhat verbose. Haml is a very popular alternative which has a condensed syntax for expressing HTML.

Is Haml better than HTML?

HAML is just a templating language that gets transformed into HTML (e.g. same final output). If you find the HAML syntax to be easier than HTML then go for it. However IMHO - abstracting away what actual elements you are generating just makes applying CSS and doing JavaScript navigation that much more difficult.


Haml with Rails 3

For Rails 3 all you need to do is add gem "haml", '3.0.25' to your Gemfile. No need to install plugin or run haml --rails ..

Just:

$ cd awesome-rails-3-app.git
$ echo 'gem "haml"' >> Gemfile

And you're done.


The answers above are spot-on. You just need to put gem 'haml' in your Gemfile.

One other tip that was not mentioned: to have rails generators use haml instead of erb, add the following to config/application.rb:

config.generators do |g|
  g.template_engine :haml

  # you can also specify a different test framework or ORM here
  # g.test_framework  :rspec
  # g.orm             :mongoid
end    

First, install haml as a gem in bundler by adding this to your Gemfile:

gem "haml"

Run bundle install, then make sure your views are named with a *.html.haml extension. For example:

`-- app
    `-- views
        |-- layouts
        |   `-- application.html.haml
        `-- users
            |-- edit.html.haml
            |-- index.html.haml
            |-- new.html.haml
            `-- show.html.haml

Add haml to your Gemfile:

gem "haml"

If you want to use the scaffold-functions too, add haml-rails within your development-group:

gem 'haml-rails', :group => :development

Don't forget to run:

$ bundle install