From 9d0ce7cddd06628dba9d15eb1366421c818a3e5a Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Wed, 28 Nov 2007 21:00:25 -0800 Subject: [PATCH] looking for layouts in files if not inline --- lib/sinatra.rb | 16 ++++++++++++++-- test/layouts_test.rb | 13 +++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index abb523d..88d254a 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -68,7 +68,7 @@ module Sinatra def render(content, options={}) @content = _evaluate_render(%Q{"#{content}"}) - layout = resolve_layout(options[:layout]) + layout = resolve_layout(options[:layout], options) @content = _evaluate_render(layout) if layout @content end @@ -81,12 +81,24 @@ module Sinatra instance_eval(content) when Proc instance_eval(&content) + when File + instance_eval(%Q{"#{content.read}"}) end end def resolve_layout(name, options={}) return if name == false - layouts[name || :layout] + if layout = layouts[name || :layout] + return layout + end + filename = (options[:views_directory] || 'views') + "/#{name}.#{ext}" + if File.file?(filename) + File.new(filename) + end + end + + def ext + :html end def layouts diff --git a/test/layouts_test.rb b/test/layouts_test.rb index b723e3b..68ce362 100644 --- a/test/layouts_test.rb +++ b/test/layouts_test.rb @@ -44,4 +44,17 @@ context "Layouts (in general)" do end + specify "can be read from a file if they're not inlined" do + + get '/foo' do + @title = 'Welcome to the Hello Program' + render 'Blake', :layout => :foo, + :views_directory => File.dirname(__FILE__) + "/views" + end + + get_it '/foo' + body.should.equal "Welcome to the Hello Program\nHi Blake\n" + + end + end -- 2.11.4.GIT