looking for layouts in files if not inline

This commit is contained in:
Blake Mizerany 2007-11-28 21:00:25 -08:00
parent 1d2a7043ef
commit 9d0ce7cddd
2 changed files with 27 additions and 2 deletions

View File

@ -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

View File

@ -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