FIX: Render without layout

This commit is contained in:
Blake Mizerany 2008-02-28 00:49:09 -08:00
parent 7d5bc1f0da
commit ccbb070300
3 changed files with 41 additions and 14 deletions

View File

@ -316,34 +316,35 @@ module Sinatra
def determine_layout(renderer, template, options)
layout_from_options = options[:layout] || :layout
layout = layouts[layout_from_options]
layout ||= resolve_template(renderer, layout_from_options, options)
layout = layouts[layout_from_options]
layout ||= resolve_template(renderer, layout_from_options, options, false)
layout
end
private
def resolve_template(renderer, template, options)
def resolve_template(renderer, template, options, scream = true)
case template
when String
template
when Proc
template.call
when Symbol
template_file(renderer, template, options)
path = File.join(
options[:views_directory] || Sinatra.application.options.views,
"#{template}.#{renderer}"
)
unless File.exists?(path)
raise Errno::ENOENT.new(path) if scream
nil
else
File.read(path)
end
else
nil
end
end
def template_file(renderer, name, options={})
path = File.join(
options[:views_directory] || Sinatra.application.options.public,
"#{name}.#{renderer}"
)
File.exists?(path) ? File.read(path) : nil
end
def layouts
Sinatra.application.layouts
end
@ -430,6 +431,7 @@ module Sinatra
:port => 4567,
:env => :development,
:root => Dir.pwd,
:views => Dir.pwd + '/views',
:public => Dir.pwd + '/public'
}
end

View File

@ -96,6 +96,10 @@ context "Haml" do
context "Templates (in general)" do
setup do
Sinatra.application = nil
end
specify "are read from files if Symbols" do
get '/from_file' do
@ -111,16 +115,36 @@ context "Haml" do
specify "use layout.ext by default if available" do
get '/layout_from_file' do
get '/' do
haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
end
get_it '/layout_from_file'
get_it '/'
should.be.ok
body.should.equal "x This is foo!\n x\n"
end
specify "renders without layout" do
get '/' do
haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
end
get_it '/'
should.be.ok
body.should.equal "<h1>No Layout!</h1>\n"
end
specify "raises error if template not found" do
get '/' do
haml :not_found
end
lambda { get_it '/' }.should.raise(Errno::ENOENT)
end
end
end

View File

@ -0,0 +1 @@
%h1 No Layout!