Ruby on rails 使用RubyonRails为haml创建一个助手或其他东西

Ruby on rails 使用RubyonRails为haml创建一个助手或其他东西,ruby-on-rails,ruby,haml,Ruby On Rails,Ruby,Haml,我在rails应用程序中使用haml,我有一个问题,如何最简单地将haml代码插入html文件: <div clas="holder"> <div class=top"></div> <div class="content"> Content into the div goes here </div> <div class="bottom"></div> </div> 有没有更简单

我在rails应用程序中使用haml,我有一个问题,如何最简单地将haml代码插入html文件:

<div clas="holder">
 <div class=top"></div>
  <div class="content">
   Content into the div goes here
  </div>
 <div class="bottom"></div>
</div>
有没有更简单的方法


我得到这个错误:

**unexpected $end, expecting kEND**
使用此代码:

# 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

解决这个问题的典型方法是使用局部搜索

或_helper.rb文件中的助手方法:

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
在哈姆:

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

你也可以使用haml_标签

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
在哈姆

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

好的,谢谢。我应该将_helper.rb放在哪里?如何加载它?对不起,我是rails的新手。刚刚使用了PHPand,我想向函数发送一个参数来更改框的颜色,它的工作原理类似于从class=“holder_@color\u here”更改div上的类,我该怎么做?对于多个版本,
open
方法已替换为
haml\u标记
。使用
haml_标签
。如果希望助手在应用程序中的任何位置都可用,请将其放入app/helpers/application.rb。如果您只希望FoosController的视图可以使用它,请将它放在app/helpers/foos.rb中=(但是在haml there nex3上做得很好!请阅读我对另一个答案的评论。从应用程序的速度来看,哪一个是最有效的?速度的差异真的很小。使用它的助手方法很棒。)。
%html
  %head
  %body
    Maybee some content here.
    = content_box do
      Content that goes in the content_box like news or stuff