From f0945409d935cdd3cb783a728d68414e7ca02dfc Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 6 Aug 2009 19:43:28 -0300 Subject: replace _render_*_from_controller with render_* as they are intended to be public --- actionpack/lib/action_view/render/rendering.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 162e38c484..86a59dd1bc 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -123,7 +123,7 @@ module ActionView layout ? _render_content_with_layout(text, layout, options[:locals]) : text end - def _render_template_from_controller(*args) + def render_template(*args) @assigns_added = nil _render_template_with_layout(*args) end -- cgit v1.2.3 From 8534c5bf193c6a234d55116d520a54a1b634a93e Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 01:51:50 -0300 Subject: Start cleaning up partial path --- actionpack/lib/action_view/render/rendering.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 86a59dd1bc..32c30e8f68 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -123,19 +123,25 @@ module ActionView layout ? _render_content_with_layout(text, layout, options[:locals]) : text end - def render_template(*args) + # 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 + # _partial:: true if the template is a partial + def render_template(options) @assigns_added = nil - _render_template_with_layout(*args) + template, layout, partial = options.values_at(:_template, :_layout, :_partial) + _render_template_with_layout(template, layout, options, partial) end - def _render_template_with_layout(template, layout = nil, options = {}, partial = false) + def _render_template_with_layout(template, layout = nil, options = {}, partial = nil) logger && logger.info("Rendering #{template.identifier}#{' (#{options[:status]})' if options[:status]}") locals = options[:locals] || {} content = if partial - object = partial unless partial == true - _render_partial_object(template, options, object) + _render_partial_object(template, options) else _render_template(template, locals) end -- cgit v1.2.3 From b3e199f6981b2fbf062fe668ff93b7dc56e98a38 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 02:46:21 -0300 Subject: Some more AV refactoring: * remove no longer used _array_like_objects * _render_content_with_layout renamed to _render_content since layout it optional * remove check for optional layout before _render_content --- actionpack/lib/action_view/render/rendering.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 32c30e8f68..527cef97c7 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -40,7 +40,7 @@ module ActionView end end - def _render_content_with_layout(content, layout, locals) + def _render_content(content, layout, locals) return content unless layout locals ||= {} @@ -116,11 +116,11 @@ module ActionView handler = Template.handler_class_for_extension(options[:type] || "erb") template = Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) content = _render_template(template, options[:locals] || {}) - layout ? _render_content_with_layout(content, layout, options[:locals]) : content + layout ? _render_content(content, layout, options[:locals]) : content end def _render_text(text, layout, options) - layout ? _render_content_with_layout(text, layout, options[:locals]) : text + layout ? _render_content(text, layout, options[:locals]) : text end # This is the API to render a ViewContext's template from a controller. @@ -146,7 +146,7 @@ module ActionView _render_template(template, locals) end - layout ? _render_content_with_layout(content, layout, locals) : content + _render_content(content, layout, locals) end end end \ No newline at end of file -- cgit v1.2.3 From 0612fd0f09977dece11a0325a0d7ee07c5cab35c Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 03:18:45 -0300 Subject: Replace _render_template_with_layout with _render_template since the layout is optional --- actionpack/lib/action_view/render/rendering.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 527cef97c7..911e480d50 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -27,7 +27,7 @@ module ActionView if file = options[:file] template = find_by_parts(file, {:formats => formats}) - _render_template_with_layout(template, layout, :locals => options[:locals]) + _render_template(template, layout, :locals => options[:locals]) elsif inline = options[:inline] _render_inline(inline, layout, options) elsif text = options[:text] @@ -54,7 +54,7 @@ module ActionView old_content, @_content_for[:layout] = @_content_for[:layout], content @cached_content_for_layout = @_content_for[:layout] - _render_template(layout, locals) + _render_single_template(layout, locals) ensure @_content_for[:layout] = old_content end @@ -97,9 +97,9 @@ module ActionView !@_content_for.key?(name) && @_proc_for_layout || @_default_layout end - def _render_template(template, local_assigns = {}) + def _render_single_template(template, locals = {}) with_template(template) do - template.render(self, local_assigns) do |*names| + template.render(self, locals) do |*names| capture(*names, &layout_proc(names.first)) end end @@ -115,7 +115,7 @@ module ActionView def _render_inline(inline, layout, options) handler = Template.handler_class_for_extension(options[:type] || "erb") template = Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) - content = _render_template(template, options[:locals] || {}) + content = _render_single_template(template, options[:locals] || {}) layout ? _render_content(content, layout, options[:locals]) : content end @@ -132,18 +132,22 @@ module ActionView def render_template(options) @assigns_added = nil template, layout, partial = options.values_at(:_template, :_layout, :_partial) - _render_template_with_layout(template, layout, options, partial) + _render_template(template, layout, options, partial) end - def _render_template_with_layout(template, layout = nil, options = {}, partial = nil) - logger && logger.info("Rendering #{template.identifier}#{' (#{options[:status]})' if options[:status]}") + def _render_template(template, layout = nil, options = {}, partial = nil) + logger && logger.info do + msg = "Rendering #{template.identifier}" + msg << " (#{options[:status]})" if options[:status] + msg + end locals = options[:locals] || {} content = if partial _render_partial_object(template, options) else - _render_template(template, locals) + _render_single_template(template, locals) end _render_content(content, layout, locals) -- cgit v1.2.3 From 59e475e3a66fe647ea74f4daf472d82bc9b4535c Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 03:48:28 -0300 Subject: Some more AV work: * rename _render_partial to _render_partial_unknown_type to reflect that for this call, we don't know the type. * Merge _render_partial_with_block and _render_partial_with_layout to _render_partial * TODO: Check to see if any more logic can be shared * TODO: See about streamlining block path so we can get rid of @_proc_for_layout * Remove @exempt_from_layout as it is no longer needed --- actionpack/lib/action_view/render/rendering.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 911e480d50..0a5953be88 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -10,24 +10,22 @@ module ActionView # # 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. - def render(options = {}, local_assigns = {}, &block) #:nodoc: - local_assigns ||= {} - - @exempt_from_layout = true - + def render(options = {}, locals = {}, &block) #:nodoc: case options + when String, NilClass + _render_partial_unknown_type(:partial => options, :locals => locals || {}) when Hash - options[:locals] ||= {} layout = options[:layout] - return _render_partial_with_layout(layout, options) if options.key?(:partial) - return _render_partial_with_block(layout, block, options) if block_given? + if options.key?(:partial) || block_given? + return _render_partial(layout, options, block) + end layout = find_by_parts(layout, {:formats => formats}) if layout if file = options[:file] template = find_by_parts(file, {:formats => formats}) - _render_template(template, layout, :locals => options[:locals]) + _render_template(template, layout, :locals => options[:locals] || {}) elsif inline = options[:inline] _render_inline(inline, layout, options) elsif text = options[:text] @@ -35,8 +33,6 @@ module ActionView end when :update update_page(&block) - when String, NilClass - _render_partial(:partial => options, :locals => local_assigns) end end -- cgit v1.2.3 From 493d84ce2f1e25081a394fd70ac4e23b6a2be682 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 05:40:01 -0300 Subject: Modify various partial methods to carry along the block that can be passed in with render * _render_single_template, which renders a template without layout * _render_partial_unknown_type, which renders a partial of unknown type (the entry method for most partial rendering; supports strings, objects, and collections) * _render_partial_object, which renders a partial for a single object. * extracted _render_partial_path so it can be used to render the spacer without going through the public render :partial --- actionpack/lib/action_view/render/rendering.rb | 33 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 0a5953be88..a721ade4e1 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -18,7 +18,7 @@ module ActionView layout = options[:layout] if options.key?(:partial) || block_given? - return _render_partial(layout, options, block) + return _render_partial(layout, options, &block) end layout = find_by_parts(layout, {:formats => formats}) if layout @@ -56,10 +56,10 @@ module ActionView end end - # You can think of a layout as a method that is called with a block. This method - # returns the block that the layout is called with. 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). + # 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). # # The user can override this default by passing a block to the layout. # @@ -88,15 +88,28 @@ module ActionView # In this case, the layout would receive the block passed into render :layout, # and the Struct specified in the layout would be passed into the block. The result # would be Hello David. - def layout_proc(name) - @_default_layout ||= proc { |*names| @_content_for[names.first || :layout] } - !@_content_for.key?(name) && @_proc_for_layout || @_default_layout + def _layout_for(names, &block) + with_output_buffer do + # This is due to the potentially ambiguous use of yield when + # a block is passed in to a template *and* there is a content_for() + # of the same name. Suggested solution: require explicit use of content_for + # in these ambiguous cases. + # + # We would be able to continue supporting yield in all non-ambiguous + # cases. Question: should we deprecate yield in favor of content_for + # and reserve yield for cases where there is a yield into a real block? + if @_content_for.key?(names.first) || !block_given? + return @_content_for[names.first || :layout] + else + return yield(names) + end + end end - def _render_single_template(template, locals = {}) + def _render_single_template(template, locals = {}, &block) with_template(template) do template.render(self, locals) do |*names| - capture(*names, &layout_proc(names.first)) + _layout_for(names, &block) end end rescue Exception => e -- cgit v1.2.3 From d94ba11295c74e5a661a78a93d6d0259ab01fa50 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 06:32:17 -0300 Subject: Continue reworking the partial path. * TODO: Review ActionController calling render_template for certain partials. Might we be able to save logic by always delegating to AV's render_partial? --- actionpack/lib/action_view/render/rendering.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index a721ade4e1..39b8608c68 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -13,12 +13,15 @@ module ActionView def render(options = {}, locals = {}, &block) #:nodoc: case options when String, NilClass - _render_partial_unknown_type(:partial => options, :locals => locals || {}) + _render_partial(:partial => options, :locals => locals || {}) when Hash layout = options[:layout] - if options.key?(:partial) || block_given? - return _render_partial(layout, options, &block) + if block_given? + return concat(_render_partial(options.merge(:partial => layout), &block)) + elsif options.key?(:partial) + layout = _pick_partial_template(layout) if layout + return _render_content(_render_partial(options), layout, options[:locals]) end layout = find_by_parts(layout, {:formats => formats}) if layout -- cgit v1.2.3 From 606e950ccbd02a10f724c73543575a2a4e1ed8cb Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 06:32:54 -0300 Subject: Whitespace --- actionpack/lib/action_view/render/rendering.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 39b8608c68..9c25fab6bb 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -16,16 +16,16 @@ module ActionView _render_partial(:partial => options, :locals => locals || {}) when Hash layout = options[:layout] - + if block_given? return concat(_render_partial(options.merge(:partial => layout), &block)) elsif options.key?(:partial) layout = _pick_partial_template(layout) if layout return _render_content(_render_partial(options), layout, options[:locals]) end - + layout = find_by_parts(layout, {:formats => formats}) if layout - + if file = options[:file] template = find_by_parts(file, {:formats => formats}) _render_template(template, layout, :locals => options[:locals] || {}) @@ -38,17 +38,17 @@ module ActionView update_page(&block) end end - + def _render_content(content, layout, locals) return content unless layout - + locals ||= {} if controller && layout @_layout = layout.identifier logger.info("Rendering template within #{layout.identifier}") if logger end - + begin old_content, @_content_for[:layout] = @_content_for[:layout], content @@ -161,7 +161,7 @@ module ActionView else _render_single_template(template, locals) end - + _render_content(content, layout, locals) end end -- cgit v1.2.3 From 010a0c92eb573cd4c216c51371356adddfde11cf Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 7 Aug 2009 15:00:12 -0300 Subject: Rename find_by_parts and find_by_parts? to find and exists? --- actionpack/lib/action_view/render/rendering.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 9c25fab6bb..742b965556 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -24,10 +24,10 @@ module ActionView return _render_content(_render_partial(options), layout, options[:locals]) end - layout = find_by_parts(layout, {:formats => formats}) if layout + layout = find(layout, {:formats => formats}) if layout if file = options[:file] - template = find_by_parts(file, {:formats => formats}) + template = find(file, {:formats => formats}) _render_template(template, layout, :locals => options[:locals] || {}) elsif inline = options[:inline] _render_inline(inline, layout, options) -- cgit v1.2.3 From d0301e13f4d6e2ddf956ecf80e85384c1ea5346c Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 8 Aug 2009 12:26:58 -0300 Subject: First pass at making partial rendering an Object. More cleanup to come. --- actionpack/lib/action_view/render/rendering.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/render/rendering.rb') diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 742b965556..c7afc56e3b 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -20,8 +20,7 @@ module ActionView if block_given? return concat(_render_partial(options.merge(:partial => layout), &block)) elsif options.key?(:partial) - layout = _pick_partial_template(layout) if layout - return _render_content(_render_partial(options), layout, options[:locals]) + return _render_partial(options) end layout = find(layout, {:formats => formats}) if layout -- cgit v1.2.3