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

Skip implicit layouts for nested templates.

That way the following will produce valid HTML:

@@ layout
!!!
= yield

@@ content
%html
  %head= haml :head
  %body= haml :body

That way using render methods for partials is a lot easier.
Tests included.
This commit is contained in:
Konstantin Haase 2010-09-19 14:56:12 +02:00
parent c98c538d53
commit e5e00471fe
6 changed files with 36 additions and 6 deletions

View file

@ -371,14 +371,18 @@ module Sinatra
options[:outvar] ||= '@_out_buf'
# extract generic options
locals = options.delete(:locals) || locals || {}
views = options.delete(:views) || settings.views || "./views"
layout = options.delete(:layout)
layout = :layout if layout.nil? || layout == true
locals = options.delete(:locals) || locals || {}
views = options.delete(:views) || settings.views || "./views"
@default_layout = :layout if @default_layout.nil?
layout = options.delete(:layout)
layout = @default_layout if layout.nil? or layout == true
# compile and render template
template = compile_template(engine, data, options, views)
output = template.render(self, locals, &block)
layout_was = @default_layout
@default_layout = false if layout
template = compile_template(engine, data, options, views)
output = template.render(self, locals, &block)
@default_layout = layout_was
# render layout
if layout

View file

@ -78,6 +78,27 @@ class TemplatesTest < Test::Unit::TestCase
assert_equal "Layout 3!\nHello World!\n", body
end
it 'avoids wrapping layouts around nested templates' do
render_app { render :str, :nested, :layout => :layout2 }
assert ok?
assert_equal "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>", body
end
it 'allows explicitly wrapping layouts around nested templates' do
render_app { render :str, :explicitly_nested, :layout => :layout2 }
assert ok?
assert_equal "<h1>String Layout!</h1>\n<content><h1>String Layout!</h1>\n<h1>Hello From String</h1></content>", body
end
it 'two independent render calls do not disable layouts' do
render_app do
render :str, :explicitly_nested, :layout => :layout2
render :str, :nested, :layout => :layout2
end
assert ok?
assert_equal "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>", body
end
it 'loads templates from source file' do
mock_app { enable :inline_templates }
assert_equal "this is foo\n\n", @app.templates[:foo][0]

View file

@ -0,0 +1 @@
<content>#{render :str, :hello, :layout => :layout2}</content>

1
test/views/hello.str Normal file
View file

@ -0,0 +1 @@
<h1>Hello From String</h1>

2
test/views/layout2.str Normal file
View file

@ -0,0 +1,2 @@
<h1>String Layout!</h1>
#{yield}

1
test/views/nested.str Normal file
View file

@ -0,0 +1 @@
<content>#{render :str, :hello}</content>