2010-03-26 2 views
23

레일에 루비 HAML위한 도우미 또는 무언가를 만들고 내가 가장 쉬운 방법은 HTML 파일에이 HAML 코드를 삽입하는 방법을 질문 있습니다내 레일 응용 프로그램과 HAML을 사용하고

<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 

답변

37

너무

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

에를 haml_tag 사용할 수 있습니다
%html 
    %head 
    %body 
    Maybee some content here. 
    = content_box do 
     Content that goes in the content_box like news or stuff 
+0

다른 답변에 대한 의견을 읽으십시오. 어느 것이 가장 많습니까? 응용 프로그램의 속도 측면에서 효과가 있습니까? – Lisinge

+0

속도의 차이는 실제로 nul입니다. 도우미 메서드는 그것을 사용하는 것이 좋습니다. – shingara

3

이 대표적인 솔루션 부분을 사용하는 것입니다.

아니면 _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 
+1

좋습니다, 감사합니다. _helper.rb는 어디에 놓아야하며 어떻게로드합니까? 죄송합니다. 새로운 레일입니다. 방금 PHP를 – Lisinge

+0

사용하고 상자의 색상을 변경하기위한 함수에 매개 변수를 보내려면 클래스에서 div = "holder_ @ color_here"클래스를 변경하는 것처럼 작동합니다, 어떻게 할 수 있습니까? – Lisinge

+4

'open' 메소드는 여러 버전에서'haml_tag'로 대체되었습니다. 대신에'haml_tag'을 사용하십시오. 도우미를 응용 프로그램의 어느 곳에서나 사용할 수있게하려면 app/helpers/application.rb에 배치하십시오. FoosController에 대한 뷰에서만 사용할 수있게하려면 app/helpers/foos.rb에 넣으십시오. –

관련 문제