2009-01-22 17:18:10 -05:00
|
|
|
module ActionView
|
|
|
|
module Rendering
|
|
|
|
# Returns the result of a render that's dictated by the options hash. The primary options are:
|
|
|
|
#
|
|
|
|
# * <tt>:partial</tt> - See ActionView::Partials.
|
|
|
|
# * <tt>:update</tt> - Calls update_page with the block given.
|
|
|
|
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
|
|
|
|
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
|
|
|
|
# * <tt>:text</tt> - Renders the text passed in out.
|
|
|
|
#
|
|
|
|
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
|
|
|
|
# as the locals hash.
|
2009-08-07 02:48:28 -04:00
|
|
|
def render(options = {}, locals = {}, &block) #:nodoc:
|
2009-01-22 17:18:10 -05:00
|
|
|
case options
|
|
|
|
when Hash
|
|
|
|
layout = options[:layout]
|
2009-10-10 06:30:25 -04:00
|
|
|
options[:locals] ||= {}
|
2009-08-07 05:32:54 -04:00
|
|
|
|
2009-08-07 05:32:17 -04:00
|
|
|
if block_given?
|
|
|
|
return concat(_render_partial(options.merge(:partial => layout), &block))
|
|
|
|
elsif options.key?(:partial)
|
2009-08-08 11:26:58 -04:00
|
|
|
return _render_partial(options)
|
2009-08-07 02:48:28 -04:00
|
|
|
end
|
2009-08-07 05:32:54 -04:00
|
|
|
|
2009-12-30 20:09:06 -05:00
|
|
|
template = if options[:file]
|
|
|
|
find(options[:file], {:formats => formats})
|
|
|
|
elsif options[:inline]
|
|
|
|
handler = Template.handler_class_for_extension(options[:type] || "erb")
|
|
|
|
Template.new(options[:inline], "inline template", handler, {})
|
|
|
|
elsif options[:text]
|
|
|
|
Template::Text.new(options[:text])
|
|
|
|
end
|
2009-08-07 05:32:54 -04:00
|
|
|
|
2009-12-30 20:09:06 -05:00
|
|
|
if template
|
|
|
|
layout = find(layout, {:formats => formats}) if layout
|
2009-10-10 06:30:25 -04:00
|
|
|
_render_template(template, layout, :locals => options[:locals])
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
when :update
|
|
|
|
update_page(&block)
|
2009-08-11 18:02:05 -04:00
|
|
|
else
|
|
|
|
_render_partial(:partial => options, :locals => locals)
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-07 04:40:01 -04:00
|
|
|
# You can think of a layout as a method that is called with a block. _layout_for
|
|
|
|
# returns the contents that are yielded to the layout. If the user calls yield
|
|
|
|
# :some_name, the block, by default, returns content_for(:some_name). If the user
|
|
|
|
# calls yield, the default block returns content_for(:layout).
|
2009-06-17 21:55:10 -04:00
|
|
|
#
|
|
|
|
# The user can override this default by passing a block to the layout.
|
|
|
|
#
|
|
|
|
# ==== Example
|
|
|
|
#
|
|
|
|
# # The template
|
|
|
|
# <% render :layout => "my_layout" do %>Content<% end %>
|
|
|
|
#
|
|
|
|
# # The layout
|
|
|
|
# <html><% yield %></html>
|
|
|
|
#
|
|
|
|
# In this case, instead of the default block, which would return content_for(:layout),
|
|
|
|
# this method returns the block that was passed in to render layout, and the response
|
|
|
|
# would be <html>Content</html>.
|
|
|
|
#
|
|
|
|
# Finally, the block can take block arguments, which can be passed in by yield.
|
|
|
|
#
|
|
|
|
# ==== Example
|
|
|
|
#
|
|
|
|
# # The template
|
2009-06-18 14:14:44 -04:00
|
|
|
# <% render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>
|
2009-06-17 21:55:10 -04:00
|
|
|
#
|
|
|
|
# # The layout
|
|
|
|
# <html><% yield Struct.new(:name).new("David") %></html>
|
|
|
|
#
|
|
|
|
# In this case, the layout would receive the block passed into <tt>render :layout</tt>,
|
|
|
|
# and the Struct specified in the layout would be passed into the block. The result
|
|
|
|
# would be <html>Hello David</html>.
|
2009-11-05 23:07:14 -05:00
|
|
|
def _layout_for(name = nil, &block)
|
2009-08-12 02:43:15 -04:00
|
|
|
return @_content_for[name || :layout] if !block_given? || name
|
2009-11-05 23:07:14 -05:00
|
|
|
capture(&block)
|
2009-06-17 21:55:10 -04:00
|
|
|
end
|
|
|
|
|
2009-08-07 00:51:50 -04:00
|
|
|
# This is the API to render a ViewContext's template from a controller.
|
|
|
|
#
|
|
|
|
# Internal Options:
|
|
|
|
# _template:: The Template object to render
|
|
|
|
# _layout:: The layout, if any, to wrap the Template in
|
|
|
|
def render_template(options)
|
2009-08-11 18:02:05 -04:00
|
|
|
_evaluate_assigns_and_ivars
|
2009-12-30 20:09:06 -05:00
|
|
|
template, layout = options.values_at(:_template, :_layout)
|
|
|
|
_render_template(template, layout, options)
|
2009-05-12 19:21:34 -04:00
|
|
|
end
|
|
|
|
|
2009-12-30 20:09:06 -05:00
|
|
|
def _render_template(template, layout = nil, options = {})
|
2009-12-25 17:37:16 -05:00
|
|
|
locals = options[:locals] || {}
|
|
|
|
|
2010-01-15 06:23:13 -05:00
|
|
|
ActiveSupport::Notifications.instrument("action_view.render_template",
|
|
|
|
:identifier => template.identifier, :layout => layout.try(:identifier)) do
|
2009-12-25 17:37:16 -05:00
|
|
|
|
2010-01-15 06:23:13 -05:00
|
|
|
content = template.render(self, locals)
|
|
|
|
@_content_for[:layout] = content
|
2009-08-11 18:02:05 -04:00
|
|
|
|
2010-01-15 06:23:13 -05:00
|
|
|
if layout
|
|
|
|
@_layout = layout.identifier
|
|
|
|
content = _render_layout(layout, locals)
|
|
|
|
end
|
2009-12-21 19:03:04 -05:00
|
|
|
|
2010-01-15 06:23:13 -05:00
|
|
|
content
|
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
2009-12-25 14:25:18 -05:00
|
|
|
|
|
|
|
def _render_layout(layout, locals, &block)
|
2010-01-15 08:45:18 -05:00
|
|
|
layout.render(self, locals){ |*name| _layout_for(*name, &block) }
|
2009-12-25 14:25:18 -05:00
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
2009-10-18 18:20:14 -04:00
|
|
|
end
|