Create a helper or something for haml with ruby on rails

Lisinge picture Lisinge · Mar 26, 2010 · Viewed 16.5k times · Source

I am using haml with my rails application and i have a question how the easiest way to insert this haml code into a html file:

<div clas="holder">
 <div class=top"></div>
  <div class="content">
   Content into the div goes here
  </div>
 <div class="bottom"></div>
</div>

And I want to use it in my haml document like this:

%html
 %head
 %body
  Maybee some content here.
  %content_box #I want to get the code i wrote inserted here
   Content that goes in the content_box like news or stuff
 %body

Is there an easier way to do this?


I get this error:

**unexpected $end, expecting kEND**

with this code:

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
 def content_box(&block)
  open :div, :class => "holder" do # haml helper
   open :div, :class => "top"
    open :div, :class => "content" do
      block.call
    open :div, :class => "bottom"
  end
 end
end

Answer

shingara picture shingara · Mar 26, 2010

You can use haml_tag too

def content_box
  haml_tag :div, :class => "holder" do
    haml_tag :div, :class => "top"
    haml_tag :div, :class => "content" do
      yield
    haml_tag :div, :class => "bottom"
  end
end

and in haml

%html
  %head
  %body
    Maybee some content here.
    = content_box do
      Content that goes in the content_box like news or stuff