From 29e45e9f4f6a2a12b873700f71c44e77083a9634 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Tue, 14 Dec 2010 22:40:42 +0100 Subject: [PATCH] Add layout_engine option to #render. This will render an erb file with a haml layout: get('/') { erb :index, :layout_engine => :haml } Whereas this will cause all markdown templates to be rendered with an erb layout (unless, of course, `:layout_engine` is set explicitly): set :markdown, :layout_engine => :erb Fixes #110. --- lib/sinatra/base.rb | 5 +++-- test/templates_test.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 680d979b..0f8ffed6 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -468,7 +468,8 @@ module Sinatra layout = options.delete(:layout) eat_errors = layout.nil? layout = @default_layout if layout.nil? or layout == true - content_type = options.delete(:content_type) || options.delete(:default_content_type) + content_type = options.delete(:content_type) || options.delete(:default_content_type) + layout_engine = options.delete(:layout_engine) || engine # compile and render template layout_was = @default_layout @@ -480,7 +481,7 @@ module Sinatra # render layout if layout options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors) - catch(:layout_missing) { output = render(engine, layout, options, locals) { output }} + catch(:layout_missing) { output = render(layout_engine, layout, options, locals) { output }} end output.extend(ContentTyped).content_type = content_type if content_type diff --git a/test/templates_test.rb b/test/templates_test.rb index 7b26819b..b12ce066 100644 --- a/test/templates_test.rb +++ b/test/templates_test.rb @@ -208,6 +208,22 @@ class TemplatesTest < Test::Unit::TestCase assert ok? assert_equal 'template in subclass', body end + + it "is possible to use a different engine for the layout than for the template itself explicitely" do + render_app do + settings.template(:layout) { 'Hello <%= yield %>!' } + render :str, "<%= 'World' %>", :layout_engine => :erb + end + assert_equal "Hello <%= 'World' %>!", body + end + + it "is possible to use a different engine for the layout than for the template itself globally" do + render_app :str => { :layout_engine => :erb } do + settings.template(:layout) { 'Hello <%= yield %>!' } + render :str, "<%= 'World' %>" + end + assert_equal "Hello <%= 'World' %>!", body + end end # __END__ : this is not the real end of the script.