Rails: Is it possible to write view helpers with HAML syntax?

From the reference:

def render_haml(code)
    engine = Haml::Engine.new(code)
    engine.render
end

This initiates a new Haml engine and renders it.


If all you are after is a method for small reusable snippets, how about partials with local variables? http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials


Haml now has a capture_haml method that you can use to accomplish this.

  def some_helper
    capture_haml do
      .some_class
        = yield
      #some-code-after
    end
  end

some_helper do
  %h1 Hello World
end
=> <div class="some_class">
     <h1>Hello World</h1>
   </div>
   <div id="some-code-after"></div>

Here is a link with more info on capture_haml: http://haml.info/docs/yardoc/Haml/Helpers.html#capture_haml-instance_method