グローバルに
erb :pageTemplate
サブセットで
erb :pageTemplate, :layout => :specificLayout
編集:
1 つの方法は、Erb またはSinatra Partialのいずれかを介してパーシャルを使用することです (私はメンテナーです。この広告ではお金を受け取りません;)
レンダリングに影響するレイアウトにフラグを渡します。
<html>
<head>
<title>Example</html>
</head>
<body>
<%= erb @specificLayout if @specificLayout %>
<%= yield %>
</body>
</html>
ルートで:
@specificLayout = :left_menu
一連のルート全体が同じフラグを必要とすることがわかった場合は、少しの継承が役立ちます。
# one big config.ru
require 'sinatra/base'
class MainController < Sinatra::Base
configure do
# lots of shared settings here
enable :inline_templates
set :specificLayout, nil
end
helpers do
# all subclasses get these too
def route
request.path
end
end
get "/" do
erb :home
end
end
class SubWithLeftMenu < MainController
configure do
set :specificLayout, :left_menu
end
get "/" do
erb :something
end
end
map( "/something" ) { run SubWithLeftMenu }
map( "/" ) { run MainController }
__END__
@@ layout
<html>
<head>
<title>Example</title>
</head>
<body>
<p>Route: <%= route %></p>
<%= erb settings.specificLayout if settings.specificLayout %>
<%= yield %>
</body>
</html>
@@ something
<p>Hello!</p>
@@ home
<p>At home</p>
@@ left_menu
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
それを実行する:
$ bin/rackup config.ru &
[1] 40556
[2013-06-21 22:16:34] INFO WEBrick 1.3.1olumes/RubyProjects/Test/nestinglayouts
[2013-06-21 22:16:34] INFO ruby 1.9.3 (2013-02-06) [x86_64-darwin10.8.0]
[2013-06-21 22:16:34] INFO WEBrick::HTTPServer#start: pid=40556 port=9292
$ curl http://localhost:9292/
127.0.0.1 - - [21/Jun/2013 22:16:47] "GET / HTTP/1.1" 200 99 0.0399
<html>
<head>
<title>Example</title>
</head>
<body>
<p>Route: /</p>
<p>At home</p>
</body>
</html>
$ curl http://localhost:9292/something/
127.0.0.1 - - [21/Jun/2013 22:16:51] "GET /something/ HTTP/1.1" 200 141 0.0064
<html>
<head>
<title>Example</title>
</head>
<body>
<p>Route: /something/</p>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<p>Hello!</p>
</body>
</html>