diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 2f809f6b..dcee2c9e 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -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 diff --git a/test/haml_test.rb b/test/haml_test.rb index 8aea5fb6..46b7d087 100644 --- a/test/haml_test.rb +++ b/test/haml_test.rb @@ -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 "

No Layout!

\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 diff --git a/test/views/no_layout/no_layout.haml b/test/views/no_layout/no_layout.haml new file mode 100644 index 00000000..0e6bd342 --- /dev/null +++ b/test/views/no_layout/no_layout.haml @@ -0,0 +1 @@ +%h1 No Layout! \ No newline at end of file