1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

add layout_options, fixes #495

This commit is contained in:
Konstantin Haase 2013-02-26 17:13:02 +11:00
parent a1f4fa9cee
commit 8a0020b2b8
4 changed files with 29 additions and 3 deletions

View file

@ -60,6 +60,9 @@
* Improve Accept header parsing, expose parameters. (Pieter van de Bruggen)
* Add `layout_options` render option. Allows you, amongst other things, to
render a layout from a different folder. (Konstantin Haase)
* Explicitly setting `layout` to `nil` is treated like setting it to `false`.
(richo)

View file

@ -386,6 +386,12 @@ Available Options:
template. Example: <tt>set :rdoc, :layout_engine => :erb</tt>
</dd>
<dt>layout_options</dt>
<dd>
Special options only used for rendering the layout. Example:
<tt>set :rdoc, :layout_options => { :views => 'views/layouts' }</tt>
</dd>
<dd>
Templates are assumed to be located directly under the `./views`
directory. To use a different views directory:

View file

@ -752,6 +752,7 @@ module Sinatra
eat_errors = layout.nil?
layout = engine_options[:layout] if layout.nil? or layout == true
layout = @default_layout if layout.nil? or layout == true
layout_options = options.delete(:layout_options) || {}
content_type = options.delete(:content_type) || options.delete(:default_content_type)
layout_engine = options.delete(:layout_engine) || engine
scope = options.delete(:scope) || self
@ -774,6 +775,7 @@ module Sinatra
# render layout
if layout
options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
options.merge! layout_options
catch(:layout_missing) { return render(layout_engine, layout, options, locals) { output } }
end

View file

@ -222,6 +222,21 @@ class TemplatesTest < Test::Unit::TestCase
assert_equal 'Hello Mike!<p>content</p>', body
end
it 'sets layout-only options via layout_options' do
mock_app do
get '/' do
render(:str, :in_a,
:views => settings.views + '/a',
:layout_options => { :views => settings.views },
:layout => :layout2)
end
end
get '/'
assert ok?
assert_equal "<h1>String Layout!</h1>\nGimme an A!\n", body
end
it 'loads templates defined in subclasses' do
base = Class.new(Sinatra::Base)
base.template(:foo) { 'bar' }