开发者

Create a helper or something for haml with ruby on rails

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="conte开发者_运维技巧nt">
   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


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


The typical solution to this is to use a partial.

Or a helper method in your _helper.rb file:

def content_box(&block)
  open :div, :class => "holder" do # haml helper
    open :div, :class => "top"
    open :div, :class => "content" do
      block.call
    end
    open :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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜