Alternatives to ERB - Haml and Markdown (also Slim)
We are going to try out Haml as an alternative templating engine. Haml is a beautiful, simple HTML preprocessor. Haml compiles directly to HTML, there’s a one to one mapping.
Haml has:
- Semantic indentation. Close tags are not necessary.
- Ruby style hashes for attributes.
Exercise
- We are going to convert one of our views into Haml. Pick one at random from your blog, you could also choose a partial.
- Add gem ‘haml’ to your Gemfile. bundle and restart your server.
- Now check out the Haml documentation here: http://haml.info/
- Manually convert your view to basic Haml
Exercise - Filters
We are going to use a markdown filter to write text in Markdown. Markdown is a simple text preprocessing language.
- Check out filters in the Haml docs
- Add a markdown filter to your template
- Now add a block of text. 2 newlines makea paragraph. A # makes an h1. ## makes an h2.
Writing text blocks in markdown can really save you time and make your ideas flow more clearly.