2010-03-26 3 views
23

मैं अपने रेल आवेदन के साथ haml उपयोग कर रहा हूँ पर गहरे लाल रंग का के साथ एक सहायक या haml के लिए कुछ बना सकते हैं और मैं एक सवाल यह है कि सबसे आसान तरीका है एक HTML फ़ाइल में इस haml कोड डालने के लिए है:रेल

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

और मैं इसे अपने हैमल दस्तावेज़ में इसका उपयोग करना चाहते हैं:

%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 

उत्तर

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

गति का अंतर वास्तव में शून्य है। सहायक उपयोग इसका उपयोग करने के लिए बहुत अच्छा है। – 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

का उपयोग कर रहा है और मैं बॉक्स के रंग को बदलने के लिए फ़ंक्शन में पैरामीटर भेजना चाहता हूं, और यह वर्ग = "holder_ @ color_here" से div पर वर्ग को बदलने जैसा काम करता है, मैं यह कैसे कर सकता हूं? – Lisinge

+4

'ओपन' विधि को कई संस्करणों के लिए 'haml_tag' के साथ बदल दिया गया है। इसके बजाए 'haml_tag' का प्रयोग करें। यदि आप चाहते हैं कि सहायक में एप्लिकेशन में कहीं भी उपलब्ध हो, तो इसे ऐप/हेल्पर्स/application.rb में रखें। यदि आप केवल FoosController के लिए दृश्यों के लिए उपलब्ध होना चाहते हैं, तो इसे ऐप/हेल्पर्स/foos.rb में रखें। –