Haml::Template options in Merb

Written . Tagged Haml, Merb, Ruby.

I’m rewriting a toy project in Merb (0.5).

Though Merb supports Haml out of the box, I was unable to find a simple way of changing the Haml options, e.g. to wrap attributes in double-quotes instead of the default single-quotes.

In Rails, you can set them in the Haml::Template.options hash, but that doesn’t work in Merb; that class is closely tied to Rails.

You can also pass options to the Haml::Engine constructor (which is what Template does). Merb doesn’t seem to offer a simple way of doing this, but Ruby itself does.

Stick this in config/merb_init.rb:

1
2
3
4
5
6
7
8
9
10
11
# Haml template configuration

class Haml::Engine
  alias_method :old_initialize, :initialize
  def initialize(template, options = {})
    options.merge!(
      :attr_wrapper => %{"}
    )
    old_initialize(template, options)
  end
end

And that’s it.