2011-08-14 9 views
10

मैं एक स्वसंपूर्ण (नहीं रेल) ​​अनुप्रयोग में इस तरह कुछ करने के लिए कोशिश कर रहा हूँ का उपयोग कर:एक स्टैंडअलोन (नहीं रेल) ​​गहरे लाल रंग का अनुप्रयोग में एक लेआउट और एक टेम्पलेट निर्दिष्ट करना, पतली या haml

layout.slim:

h1 Hello 
.content 
    = yield 

show.slim:

= object.name 
= object.description 

मैं एक लेआउट और एक टेम्पलेट निर्दिष्ट करने का तरीका को समझ नहीं सकता। क्या यह पतला (या हैमल) के साथ संभव है? धन्यवाद।

उत्तर

18

layout.slim फ़ाइल लगता है:

h1 Hello 
.content 
    == yield 

contents.slim फ़ाइल की तरह दिखता है:

= name 

यह छोटा किया जा सकता है, लेकिन मैं स्पष्टीकरण प्रयोजनों के लिए अलग-अलग चरणों के लिए अलग कर दिया।

require 'slim' 

# Simple class to represent an environment 
class Env 
    attr_accessor :name 
end 

# Intialize it 
env = Env.new 
# Set the variable we reference in contents.slim 
env.name = "test this layout" 

# Read the layout file in as a string 
layout = File.open("layout.slim", "rb").read 

# Read the contents file in as a string 
contents = File.open("contents.slim", "rb").read 

# Create new template object with the layout 
l = Slim::Template.new { layout } 

# Render the contents passing in the environment: env 
# so that it can resolve: = name 
c = Slim::Template.new { contents }.render(env) 

# Render the layout passing it the rendered contents 
# as the block. This is what yield in layout.slim will get 
puts l.render{ c } 

हो जाएगा ताकि उत्पादन:

<h1>Hello</h1><div class="content">test this layout</div> 
+0

धन्यवाद! मैं दस्तावेज़ों से बहुत करीब था लेकिन मैं इस बात के बारे में उलझन में था कि क्या गुंजाइश थी। मुझे '== उपज'' उपज' – chrismealy

+0

के बजाय भी करना था, ओह हाँ, इसके बारे में खेद है, स्लिम डिफ़ॉल्ट रूप से HTML से बच निकलता है। मैंने कोड अपडेट किया। – stonean

+1

'File.open ("content.slim", "rb") के बजाय' File.read ("layout.slim") 'का उपयोग करें। पढ़ें'। उत्तरार्द्ध फ़ाइल को बंद करने के लिए जीसी/फाइनलाइज़र पर निर्भर करता है जो कभी भी हो सकता है या नहीं भी हो सकता है। – apeiros

संबंधित मुद्दे