ruby-on-rails - 使用 ruby​​ on rails 为 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>

我想像这样在我的 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

有更简单的方法吗?


我收到这个错误:

**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

最佳答案

你也可以使用 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

在火腿里

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

关于ruby-on-rails - 使用 ruby​​ on rails 为 haml 创建一个助手或其他东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2523668/

相关文章:

ruby-on-rails - 关于渲染文件大小的奇怪行为

ruby-on-rails - 使用 RSpec 和模拟测试 after_commit

ruby-on-rails - 未定义的方法每个 rails 错误

ruby - 如何理解 ruby​​ 枚举器链中的工作流程

css - Devise 不加载自定义 CSS

mysql - Rails 集成测试:Mysql2::Error: 无法连接到 MySQL 服务器

ruby-on-rails - capybara 不点击链接

haml - 删除 HAML 中的空格

ruby - 在 haml & serve 中,重复元素或 block 数次

ruby-on-rails - 需要帖子的 Rails url/:id/the-name-of-post