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.
This commit is contained in:
Konstantin Haase 2010-12-14 22:40:42 +01:00
parent 97209b5e3e
commit 29e45e9f4f
2 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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.