2010-01-22 10:46:45 -05:00
|
|
|
require 'active_support/core_ext/object/try'
|
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
module ActionView
|
2010-06-20 16:26:31 -04:00
|
|
|
# = Action View Rendering
|
2009-01-22 17:18:10 -05:00
|
|
|
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.
|
2010-10-10 06:40:47 -04:00
|
|
|
# * <tt>:once</tt> - Accepts a string or an array of strings and Rails will ensure they each of them are rendered just once.
|
2009-01-22 17:18:10 -05:00
|
|
|
#
|
|
|
|
# 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.
|
2010-03-12 14:39:53 -05:00
|
|
|
def render(options = {}, locals = {}, &block)
|
2009-01-22 17:18:10 -05:00
|
|
|
case options
|
|
|
|
when Hash
|
2009-08-07 05:32:17 -04:00
|
|
|
if block_given?
|
Add deprecation notices for <% %>.
* The approach is to compile <% %> into a method call that checks whether
the value returned from a block is a String. If it is, it concats to the buffer and
prints a deprecation warning.
* <%= %> uses exactly the same logic to compile the template, which first checks
to see whether it's compiling a block.
* This should have no impact on other uses of block in templates. For instance, in
<% [1,2,3].each do |i| %><%= i %><% end %>, the call to each returns an Array,
not a String, so the result is not concatenated
* In two cases (#capture and #cache), a String can be returned that should *never*
be concatenated. We have temporarily created a String subclass called NonConcattingString
which behaves (and is serialized) identically to String, but is not concatenated
by the code that handles deprecated <% %> block helpers. Once we remove support
for <% %> block helpers, we can remove NonConcattingString.
2010-03-15 14:58:05 -04:00
|
|
|
_render_partial(options.merge(:partial => options[:layout]), &block)
|
2010-03-12 14:39:53 -05:00
|
|
|
elsif options.key?(:partial)
|
|
|
|
_render_partial(options)
|
2010-10-10 06:34:31 -04:00
|
|
|
elsif options.key?(:once)
|
|
|
|
_render_once(options)
|
2010-03-08 08:46:57 -05:00
|
|
|
else
|
2010-10-10 05:03:18 -04:00
|
|
|
_render_template(options)
|
2009-08-07 02:48:28 -04:00
|
|
|
end
|
2009-01-22 17:18:10 -05:00
|
|
|
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
|
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
# Returns the contents that are yielded to a layout, given a name or a block.
|
|
|
|
#
|
|
|
|
# You can think of a layout as a method that is called with a block. If the user calls
|
|
|
|
# <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
|
|
|
|
# If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
|
|
|
|
#
|
|
|
|
# The user can override this default by passing a block to the layout:
|
|
|
|
#
|
|
|
|
# # 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 <tt>content_for(:layout)</tt>,
|
|
|
|
# this method returns the block that was passed in to <tt>render :layout</tt>, and the response
|
|
|
|
# would be
|
|
|
|
#
|
|
|
|
# <html>
|
|
|
|
# Content
|
|
|
|
# </html>
|
|
|
|
#
|
|
|
|
# Finally, the block can take block arguments, which can be passed in by +yield+:
|
|
|
|
#
|
|
|
|
# # The template
|
|
|
|
# <%= render :layout => "my_layout" do |customer| %>
|
|
|
|
# Hello <%= customer.name %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
|
|
|
# # 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 would be passed into the block as an argument. The result
|
|
|
|
# would be
|
|
|
|
#
|
|
|
|
# <html>
|
|
|
|
# Hello David
|
|
|
|
# </html>
|
|
|
|
#
|
|
|
|
def _layout_for(*args, &block)
|
|
|
|
name = args.first
|
2010-10-07 14:48:21 -04:00
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
if name.is_a?(Symbol)
|
|
|
|
@_content_for[name].html_safe
|
|
|
|
elsif block
|
|
|
|
capture(*args, &block)
|
2010-10-07 14:48:21 -04:00
|
|
|
else
|
2010-10-10 05:03:18 -04:00
|
|
|
@_content_for[:layout].html_safe
|
2010-10-07 14:48:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-10 06:34:31 -04:00
|
|
|
def _render_once(options) #:nodoc:
|
|
|
|
_template_renderer.render_once(options)
|
|
|
|
end
|
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
def _render_template(options) #:nodoc:
|
|
|
|
_template_renderer.render(options)
|
2009-05-12 19:21:34 -04:00
|
|
|
end
|
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
def _template_renderer #:nodoc:
|
|
|
|
@_template_renderer ||= TemplateRenderer.new(self)
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
end
|
2009-10-18 18:20:14 -04:00
|
|
|
end
|