开发者

Create a helper or something for haml with ruby on rails

开发者 https://www.devze.com 2022-12-24 14:28 出处:网络
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:

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

精彩评论

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

关注公众号