Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Recognize Path-like objects in Templates#compile_template.

  • Loading branch information...
commit 985f11b51076f59b2b37f3bb1d4767c157df0035 1 parent 8752085
@blambeau authored
Showing with 31 additions and 1 deletion.
  1. +8 −1 lib/sinatra/base.rb
  2. +23 −0 test/templates_test.rb
View
9 lib/sinatra/base.rb
@@ -738,7 +738,14 @@ def compile_template(engine, data, options, views)
path, line = settings.caller_locations.first
template.new(path, line.to_i, options, &body)
else
- raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
+ if data.respond_to?(:path) and path = data.path
+ path = File.absolute_path(path, views)
+ throw :layout_missing unless path.index(File.expand_path(views))==0
+ throw :layout_missing if eat_errors and not File.exists?(path)
+ template.new(path, 1, options)
+ else
+ raise ArgumentError, "Sorry, don't know how to render #{data.inspect}."
+ end
end
end
end
View
23 test/templates_test.rb
@@ -14,6 +14,8 @@ def evaluate(scope, locals={}, &block)
Tilt.register 'test', self
end
+Pathlike = Struct.new(:path)
+
class TemplatesTest < Test::Unit::TestCase
def render_app(base=Sinatra::Base, options = {}, &block)
base, options = Sinatra::Base, base if base.is_a? Hash
@@ -52,6 +54,27 @@ def with_default_layout
assert_equal "Hello World!\n", body
end
+ it 'renders Path-like objects given a relative path to views' do
+ render_app{ render(:test, Pathlike.new('hello.test')) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+
+ it 'renders Path-like objects given an absolute path' do
+ render_app{ render(:test, Pathlike.new(File.expand_path("../views/hello.test", __FILE__))) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+
+ it 'does not render Path-like objects outside views' do
+ mock_app do
+ get '/' do
+ render :test, Pathlike.new(File.expand_path(__FILE__))
+ end
+ end
+ assert_throws(:layout_missing){ get '/' }
+ end
+
it 'uses the default layout template if not explicitly overridden' do
with_default_layout do
render_app { render(:test, :hello) }
Please sign in to comment.
Something went wrong with that request. Please try again.