Skip to content
This repository
Browse code

Drive the final stake through @content_for_*'s heart!

  • Loading branch information...
commit a9ad763c86e110c280be0b7a763496f9e1204de0 1 parent 65102c8
authored June 17, 2009
3  actionpack/lib/action_controller/legacy/layout.rb
@@ -44,9 +44,6 @@ def self.included(base)
44 44
     #   hello world
45 45
     #   // The footer part of this layout
46 46
     #
47  
-    # NOTE: The old notation for rendering the view from a layout was to expose the magic <tt>@content_for_layout</tt> instance
48  
-    # variable. The preferred notation now is to use <tt>yield</tt>, as documented above.
49  
-    #
50 47
     # == Accessing shared variables
51 48
     #
52 49
     # Layouts have access to variables specified in the content pages and vice versa. This allows you to have layouts with
4  actionpack/lib/action_view/base.rb
@@ -170,12 +170,13 @@ class Base
170 170
 
171 171
     attr_accessor :base_path, :assigns, :template_extension, :formats
172 172
     attr_accessor :controller
  173
+    attr_internal :captures
173 174
 
174 175
     attr_accessor :output_buffer
175 176
 
176 177
     class << self
177 178
       delegate :erb_trim_mode=, :to => 'ActionView::TemplateHandlers::ERB'
178  
-      delegate :logger, :to => 'ActionController::Base'
  179
+      delegate :logger, :to => 'ActionController::Base', :allow_nil => true
179 180
     end
180 181
 
181 182
     @@debug_rjs = false
@@ -232,6 +233,7 @@ def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil,
232 233
       @assigns = assigns_for_first_render
233 234
       @controller = controller
234 235
       @helpers = ProxyModule.new(self)
  236
+      @_content_for = Hash.new {|h,k| h[k] = "" }
235 237
       self.view_paths = view_paths
236 238
     end
237 239
 
5  actionpack/lib/action_view/helpers/capture_helper.rb
@@ -116,10 +116,9 @@ def capture(*args, &block)
116 116
       # named <tt>@content_for_#{name_of_the_content_block}</tt>. The preferred usage is now
117 117
       # <tt><%= yield :footer %></tt>.
118 118
       def content_for(name, content = nil, &block)
119  
-        ivar = "@content_for_#{name}"
120 119
         content = capture(&block) if block_given?
121  
-        instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}")
122  
-        nil
  120
+        return @_content_for[name] << content if content
  121
+        @_content_for[name]
123 122
       end
124 123
 
125 124
       # Use an alternate output buffer for the duration of the block.
7  actionpack/lib/action_view/render/partials.rb
@@ -245,13 +245,6 @@ def _deprecated_ivar_assign(template)
245 245
         end
246 246
       end
247 247
 
248  
-      def _render_partial_with_block(layout, block, options)
249  
-        @_proc_for_layout = block
250  
-        concat(_render_partial(options.merge(:partial => layout)))
251  
-      ensure
252  
-        @_proc_for_layout = nil
253  
-      end
254  
-  
255 248
       def _render_partial_with_layout(layout, options)
256 249
         if layout
257 250
           prefix = controller && !layout.include?("/") ? controller.controller_path : nil
32  actionpack/lib/action_view/render/rendering.rb
@@ -51,13 +51,12 @@ def _render_content_with_layout(content, layout, locals)
51 51
       end
52 52
   
53 53
       begin
54  
-        original_content_for_layout = @content_for_layout if defined?(@content_for_layout)
55  
-        @content_for_layout = content
  54
+        old_content, @_content_for[:layout] = @_content_for[:layout], content
56 55
 
57  
-        @cached_content_for_layout = @content_for_layout
  56
+        @cached_content_for_layout = @_content_for[:layout]
58 57
         _render_template(layout, locals)
59 58
       ensure
60  
-        @content_for_layout = original_content_for_layout
  59
+        @_content_for[:layout] = old_content
61 60
       end
62 61
     end
63 62
 
@@ -66,12 +65,11 @@ def _render_template(template, local_assigns = {})
66 65
         _evaluate_assigns_and_ivars
67 66
 
68 67
         template.render(self, local_assigns) do |*names|
69  
-          if !instance_variable_defined?(:"@content_for_#{names.first}") && 
70  
-          instance_variable_defined?(:@_proc_for_layout) && (proc = @_proc_for_layout)
71  
-            capture(*names, &proc)
72  
-          elsif instance_variable_defined?(ivar = :"@content_for_#{names.first || :layout}")
73  
-            instance_variable_get(ivar)
74  
-          end        
  68
+          if !@_content_for.key?(names.first) && @_proc_for_layout
  69
+            capture(*names, &@_proc_for_layout)
  70
+          elsif content = @_content_for[names.first || :layout]
  71
+            content
  72
+          end
75 73
         end
76 74
       end
77 75
     rescue Exception => e
@@ -100,20 +98,18 @@ def _render_template_from_controller(*args)
100 98
     end
101 99
 
102 100
     def _render_template_with_layout(template, layout = nil, options = {}, partial = false)
103  
-      if controller && logger
104  
-        logger.info("Rendering #{template.identifier}" + 
105  
-          (options[:status] ? " (#{options[:status]})" : ''))
106  
-      end
107  
-  
  101
+      logger && logger.info("Rendering #{template.identifier}#{' (#{options[:status]})' if options[:status]}")
  102
+
  103
+      locals = options[:locals] || {}
  104
+
108 105
       content = if partial
109 106
         object = partial unless partial == true
110 107
         _render_partial_object(template, options, object)
111 108
       else
112  
-        _render_template(template, options[:locals] || {})
  109
+        _render_template(template, locals)
113 110
       end
114 111
   
115  
-      return content unless layout
116  
-      _render_content_with_layout(content, layout, options[:locals] || {})
  112
+      layout ? _render_content_with_layout(content, layout, locals) : content
117 113
     end
118 114
   end
119 115
 end
2  actionpack/test/fixtures/layouts/builder.builder
... ...
@@ -1,3 +1,3 @@
1 1
 xml.wrapper do
2  
-  xml << @content_for_layout
  2
+  xml << yield
3 3
 end
2  actionpack/test/fixtures/layouts/standard.html.erb
... ...
@@ -1 +1 @@
1  
-<html><%= @content_for_layout %><%= @variable_for_layout %></html>
  1
+<html><%= yield %><%= @variable_for_layout %></html>
4  actionpack/test/fixtures/layouts/talk_from_action.erb
... ...
@@ -1,2 +1,2 @@
1  
-<title><%= @title || @content_for_title %></title>
2  
-<%= @content_for_layout -%>
  1
+<title><%= @title || yield(:title) %></title>
  2
+<%= yield -%>

0 notes on commit a9ad763

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