在rails上使用ruby为haml创建一个帮助器或东西

Lis*_*nge 23 ruby haml ruby-on-rails

我正在使用haml和我的rails应用程序,我有一个问题如何将这个haml代码插入到html文件中的最简单方法:

<div clas="holder">
 <div class=top"></div>
  <div class="content">
   Content into the div goes here
  </div>
 <div class="bottom"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

我想在我的haml文档中使用它,如下所示:

%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
Run Code Online (Sandbox Code Playgroud)

有更简单的方法吗?


我收到此错误:

**unexpected $end, expecting kEND**
Run Code Online (Sandbox Code Playgroud)

使用此代码:

# 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
Run Code Online (Sandbox Code Playgroud)

shi*_*ara 37

你也可以使用haml_tag

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
Run Code Online (Sandbox Code Playgroud)

并以haml

%html
  %head
  %body
    Maybee some content here.
    = content_box do
      Content that goes in the content_box like news or stuff
Run Code Online (Sandbox Code Playgroud)