14

スタンドアロン(レールではない)アプリで次のようなことをしようとしています:

レイアウト.スリム:

h1 Hello
.content
  = yield

show.slim:

= object.name
= object.description

レイアウトとテンプレートの指定方法がわかりません。これはスリム(またはハムル)で可能ですか?ありがとう。

4

1 に答える 1

23

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>
于 2011-08-14T20:17:52.253 に答える