2010-03-26 25 views
23

raylar üzerinde yakut ile Haml için bir yardımcı ya da bir şey oluşturma ve ben en kolay yolu html dosyası içine bu haml kodu eklemek nasıl bir sorum var:benim raylar uygulama ile Haml kullanıyorum

<div clas="holder"> 
<div class=top"></div> 
    <div class="content"> 
    Content into the div goes here 
    </div> 
<div class="bottom"></div> 
</div> 

Ve haml belgesinde şu şekilde kullanmak istiyorum:

%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 

Bunu yapmanın daha kolay bir yolu var mı? Bu kod ile

**unexpected $end, expecting kEND** 

:


bu hata olsun

# 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 

cevap

37

Sen de

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 

ve Haml

yılında haml_tag kullanabilirsiniz
%html 
    %head 
    %body 
    Maybee some content here. 
    = content_box do 
     Content that goes in the content_box like news or stuff 
+0

lütfen diğer cevaba yaptığım yorumu okuyun ve hangisi en çok Uygulamanın hızı açısından etkin mi? – Lisinge

+0

hız farkı gerçekten nul.Yardımcı yöntemi kullanmak harika. – shingara

3

Buna tipik bir çözelti, kısmi kullanmaktır.

Veya _helper.rb dosyasında bir yardımcı yöntemi:

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 

Ve Haml içinde

:

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

tamam, teşekkürler. ve nerede _helper.rb koyarım ve nasıl yüklerim? Üzgünüz, raylara yeni çıkıyorum. Sadece PHP – Lisinge

+0

kullanarak ve ben kutunun rengini değiştirmek için işleve bir parametre göndermek istediğiniz ve bu ı bunu nasıl class = "holder_ @ color_here" dan div üzerinde sınıf değiştirme gibi çalışır mu? – Lisinge

+4

'open' yöntemi birkaç versiyon için' haml_tag' ile değiştirildi. Bunun yerine 'haml_tag' kullanın. Eğer yardımcı yerde uygulamada kullanılabilir olması app/yardımcıları/application.rb koymak istiyorum. Sadece FoosController için görüntüleme için kullanılabilir olmasını istiyorsanız, onu app/helpers/foos.rb içine koyun. –