mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
FIX: Render without layout
This commit is contained in:
parent
7d5bc1f0da
commit
ccbb070300
3 changed files with 41 additions and 14 deletions
|
@ -317,33 +317,34 @@ 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 ||= 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
|
||||
|
|
|
@ -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
|
||||
|
|
1
test/views/no_layout/no_layout.haml
Normal file
1
test/views/no_layout/no_layout.haml
Normal file
|
@ -0,0 +1 @@
|
|||
%h1 No Layout!
|
Loading…
Reference in a new issue