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>
धन्यवाद! मैं दस्तावेज़ों से बहुत करीब था लेकिन मैं इस बात के बारे में उलझन में था कि क्या गुंजाइश थी। मुझे '== उपज'' उपज' – chrismealy
के बजाय भी करना था, ओह हाँ, इसके बारे में खेद है, स्लिम डिफ़ॉल्ट रूप से HTML से बच निकलता है। मैंने कोड अपडेट किया। – stonean
'File.open ("content.slim", "rb") के बजाय' File.read ("layout.slim") 'का उपयोग करें। पढ़ें'। उत्तरार्द्ध फ़ाइल को बंद करने के लिए जीसी/फाइनलाइज़र पर निर्भर करता है जो कभी भी हो सकता है या नहीं भी हो सकता है। – apeiros