Skip to content
Browse files

Promote `render` in public API, with generalized signature.

A private method `_render(view, options, &block)` replaces the previous
render method. The engine to use can be specified under options[:engine],
either as a Tilt::Template subclass or a name. When not specified,
`_render` infers the engine to use from the `view` parameter when possible.

`render` is promoted in the public API to allow clients to request rendering
while relying on engine inference. The signature has been generalized
accordingly.
  • Loading branch information...
1 parent 53846ac commit 1df6fe68a4a563374ca4928254308a34866a0282 @blambeau blambeau committed
Showing with 24 additions and 10 deletions.
  1. +16 −10 lib/sinatra/base.rb
  2. +8 −0 test/templates_test.rb
View
26 lib/sinatra/base.rb
@@ -696,6 +696,13 @@ def find_view_location(view, greedy)
[path, 1]
end
+ def render(engine, view=nil, options={}, locals={}, &block)
+ engine, view, options, locals = nil, engine, view || {}, options if view.nil? or view.is_a?(Hash)
+ options[:engine] = engine
+ options[:locals] ||= locals
+ _render(view, options, &block)
+ end
+
private
# logic shared between builder and nokogiri
def render_ruby(engine, template, options={}, locals={}, &block)
@@ -704,21 +711,20 @@ def render_ruby(engine, template, options={}, locals={}, &block)
render engine, template, options, locals
end
- def render(engine, view, options={}, locals={}, &block)
- _render(view, options.merge(:engine => engine), locals, &block)
- end
-
- def _render(view, options={}, locals={}, &block)
+ def _render(view, options={}, &block)
# get the engine to use
- engine = options[:engine]
+ engine = options[:engine] || find_view_engine(view, options)
# merge app-level options
- options = settings.send(engine).merge(options) if settings.respond_to?(engine)
+ if engine.is_a?(Symbol) and settings.respond_to?(engine)
+ options = settings.send(engine).merge(options)
+ end
+
options[:outvar] ||= '@_out_buf'
options[:default_encoding] ||= settings.default_encoding
# extract generic options
- locals = options.delete(:locals) || locals || {}
+ locals = options.delete(:locals) || {}
views = options.delete(:views) || settings.views || "./views"
layout = options.delete(:layout)
eat_errors = layout.nil?
@@ -740,8 +746,8 @@ def _render(view, options={}, locals={}, &block)
# render layout
if layout
options = options.merge(:engine => layout_engine, :views => views, :layout => false,
- :eat_errors => eat_errors, :scope => scope)
- catch(:layout_missing) { return _render(layout, options, locals) { output } }
+ :eat_errors => eat_errors, :scope => scope, :locals => locals)
+ catch(:layout_missing) { return _render(layout, options) { output } }
end
output.extend(ContentTyped).content_type = content_type if content_type
View
8 test/templates_test.rb
@@ -68,6 +68,14 @@ def with_hello_paths
end
end
+ it 'infers the engine to use from path extension' do
+ with_hello_paths do |path|
+ render_app{ render(path) }
+ assert ok?
+ assert_equal "Hello World!\n", body
+ end
+ end
+
it 'uses the default layout template if not explicitly overridden' do
with_default_layout do
render_app { render(:test, :hello) }

0 comments on commit 1df6fe6

Please sign in to comment.
Something went wrong with that request. Please try again.