全球
erb :pageTemplate
在子集中
erb :pageTemplate, :layout => :specificLayout
编辑:
一种方法是通过 Erb 或Sinatra Partial使用 partials (我是维护者,我没有为这个广告赚钱;)
将标志传递给影响渲染的布局:
<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>