From 1e0a6472717f6ee5054ddd1e67d1af41b4f80c91 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Fri, 14 Sep 2012 00:29:58 +0200 Subject: [PATCH] allow engine specific layout to be true, too --- lib/sinatra/base.rb | 13 ++++++++----- test/templates_test.rb | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 47ba639e..b0cf6aee 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -679,21 +679,24 @@ module Sinatra def render(engine, data, options={}, locals={}, &block) # merge app-level options - options = settings.send(engine).merge(options) if settings.respond_to?(engine) - options[:outvar] ||= '@_out_buf' - options[:default_encoding] ||= settings.default_encoding + engine_options = settings.respond_to?(engine) ? settings.send(engine) : {} + options = engine_options.merge(options) # extract generic options locals = options.delete(:locals) || locals || {} views = options.delete(:views) || settings.views || "./views" layout = options.delete(:layout) eat_errors = layout.nil? - engine_layout = settings.send(engine)[:layout] if settings.respond_to?(engine) - layout = engine_layout || @default_layout if layout.nil? or layout == true + layout = engine_options[:layout] if layout.nil? or layout == true + layout = @default_layout if layout.nil? or layout == true content_type = options.delete(:content_type) || options.delete(:default_content_type) layout_engine = options.delete(:layout_engine) || engine scope = options.delete(:scope) || self + # set some defaults + options[:outvar] ||= '@_out_buf' + options[:default_encoding] ||= settings.default_encoding + # compile and render template begin layout_was = @default_layout diff --git a/test/templates_test.rb b/test/templates_test.rb index e4032c15..6910b3d5 100644 --- a/test/templates_test.rb +++ b/test/templates_test.rb @@ -49,6 +49,21 @@ class TemplatesTest < Test::Unit::TestCase assert_equal "Layout 3!Hello World!", body end + it 'falls back to default layout if engine layout is true' do + mock_app do + template(:layout) { 'Layout!!! <%= yield %>' } + set :erb, :layout => true + + get('/') do + erb('Hello World!', { :layout => true }) + end + end + + get '/' + assert ok? + assert_equal "Layout!!! Hello World!", body + end + it 'renders String templates directly' do render_app { render(:test, 'Hello World') } assert ok?