Подтвердить что ты не робот

Создайте помощника или что-нибудь для haml с рубином на рельсах

Я использую 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>

И я хочу использовать его в своем документе 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
4b9b3361

Ответ 1

Вы также можете использовать 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

и в haml

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

Ответ 2

Типичным решением для этого является использование частичного.

Или вспомогательный метод в файле _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

И в haml:

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