layouts and templates from files
This commit is contained in:
parent
9d0ce7cddd
commit
eaa3227fed
|
@ -67,7 +67,8 @@ module Sinatra
|
||||||
module RenderingHelpers
|
module RenderingHelpers
|
||||||
|
|
||||||
def render(content, options={})
|
def render(content, options={})
|
||||||
@content = _evaluate_render(%Q{"#{content}"})
|
template = resolve_template(content, options)
|
||||||
|
@content = _evaluate_render(template)
|
||||||
layout = resolve_layout(options[:layout], options)
|
layout = resolve_layout(options[:layout], options)
|
||||||
@content = _evaluate_render(layout) if layout
|
@content = _evaluate_render(layout) if layout
|
||||||
@content
|
@content
|
||||||
|
@ -78,13 +79,23 @@ module Sinatra
|
||||||
def _evaluate_render(content, options={})
|
def _evaluate_render(content, options={})
|
||||||
case content
|
case content
|
||||||
when String
|
when String
|
||||||
instance_eval(content)
|
instance_eval(%Q{"#{content}"})
|
||||||
when Proc
|
when Proc
|
||||||
instance_eval(&content)
|
instance_eval(&content)
|
||||||
when File
|
when File
|
||||||
instance_eval(%Q{"#{content.read}"})
|
instance_eval(%Q{"#{content.read}"})
|
||||||
end
|
end
|
||||||
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={})
|
def resolve_layout(name, options={})
|
||||||
return if name == false
|
return if name == false
|
||||||
|
|
|
@ -48,7 +48,7 @@ context "Layouts (in general)" do
|
||||||
|
|
||||||
get '/foo' do
|
get '/foo' do
|
||||||
@title = 'Welcome to the Hello Program'
|
@title = 'Welcome to the Hello Program'
|
||||||
render 'Blake', :layout => :foo,
|
render 'Blake', :layout => :foo_layout,
|
||||||
:views_directory => File.dirname(__FILE__) + "/views"
|
:views_directory => File.dirname(__FILE__) + "/views"
|
||||||
end
|
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