layouts and templates from files
This commit is contained in:
parent
9d0ce7cddd
commit
eaa3227fed
|
@ -67,7 +67,8 @@ module Sinatra
|
|||
module RenderingHelpers
|
||||
|
||||
def render(content, options={})
|
||||
@content = _evaluate_render(%Q{"#{content}"})
|
||||
template = resolve_template(content, options)
|
||||
@content = _evaluate_render(template)
|
||||
layout = resolve_layout(options[:layout], options)
|
||||
@content = _evaluate_render(layout) if layout
|
||||
@content
|
||||
|
@ -78,7 +79,7 @@ module Sinatra
|
|||
def _evaluate_render(content, options={})
|
||||
case content
|
||||
when String
|
||||
instance_eval(content)
|
||||
instance_eval(%Q{"#{content}"})
|
||||
when Proc
|
||||
instance_eval(&content)
|
||||
when File
|
||||
|
@ -86,6 +87,16 @@ module Sinatra
|
|||
end
|
||||
end
|
||||
|
||||
def resolve_template(content, options={})
|
||||
case content
|
||||
when String
|
||||
content
|
||||
when Symbol
|
||||
filename = (options[:views_directory] || 'views') + "/#{content}.#{ext}"
|
||||
File.new(filename)
|
||||
end
|
||||
end
|
||||
|
||||
def resolve_layout(name, options={})
|
||||
return if name == false
|
||||
if layout = layouts[name || :layout]
|
||||
|
|
|
@ -48,7 +48,7 @@ context "Layouts (in general)" do
|
|||
|
||||
get '/foo' do
|
||||
@title = 'Welcome to the Hello Program'
|
||||
render 'Blake', :layout => :foo,
|
||||
render 'Blake', :layout => :foo_layout,
|
||||
:views_directory => File.dirname(__FILE__) + "/views"
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
context "Templates (in general)" do
|
||||
|
||||
specify "are read from files if Symbols" do
|
||||
|
||||
get '/from_file' do
|
||||
@name = 'Alena'
|
||||
render :foo, :views_directory => File.dirname(__FILE__) + "/views"
|
||||
end
|
||||
|
||||
get_it '/from_file'
|
||||
|
||||
body.should.equal 'You rock Alena!'
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
You rock #{@name}!
|
|
@ -0,0 +1,2 @@
|
|||
#{@title}
|
||||
Hi #{@content}
|
Loading…
Reference in New Issue