Skip to content
This repository
Browse code

Fixed CaptureHelper#content_for to work with the optional content par…

…ameter instead of just the block #9434 [sandofsky/wildchild]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7522 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 415649760226a7987ba1f8d234c1785d0fa333b0 1 parent 0d5a7d1
David Heinemeier Hansson authored September 21, 2007
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed CaptureHelper#content_for to work with the optional content parameter instead of just the block #9434 [sandofsky/wildchild].
  4
+
3 5
 * Added Mime::Type.register_alias for dealing with different formats using the same mime type [DHH]. Example:
4 6
 
5 7
     class PostsController < ApplicationController
12  actionpack/lib/action_view/helpers/capture_helper.rb
@@ -93,7 +93,7 @@ def capture(*args, &block)
93 93
       # That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists)
94 94
       # on the page; this technique is useful if you'll only be using these scripts in a few views.
95 95
       #
96  
-      # Also, note that content_for concatenates the blocks it is given for a particular
  96
+      # Note that content_for concatenates the blocks it is given for a particular
97 97
       # identifier in order. For example:
98 98
       #
99 99
       #   <% content_for :navigation do %>
@@ -109,7 +109,11 @@ def capture(*args, &block)
109 109
       # Then, in another template or layout, this code would render both links in order:
110 110
       #
111 111
       #   <ul><%= yield :navigation %></ul>
112  
-      #      
  112
+      #
  113
+      # Lastly, simple content can be passed as a parameter:
  114
+      #
  115
+      #   <% content_for :script, javascript_include_tag(:defaults) %>
  116
+      #
113 117
       # WARNING: content_for is ignored in caches. So you shouldn't use it
114 118
       # for elements that will be fragment cached.
115 119
       #
@@ -118,7 +122,9 @@ def capture(*args, &block)
118 122
       # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred usage is now
119 123
       # <tt><%= yield :footer %></tt>.
120 124
       def content_for(name, content = nil, &block)
121  
-        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"
  125
+        existing_content_for = instance_variable_get("@content_for_#{name}").to_s
  126
+        new_content_for      = existing_content_for + (block_given? ? capture(&block) : content)
  127
+        instance_variable_set("@content_for_#{name}", new_content_for)
122 128
       end
123 129
 
124 130
       private
20  actionpack/test/controller/capture_test.rb
@@ -8,6 +8,14 @@ def content_for
8 8
     render :layout => "talk_from_action"
9 9
   end
10 10
 
  11
+  def content_for_with_parameter
  12
+    render :layout => "talk_from_action"
  13
+  end
  14
+  
  15
+  def content_for_concatenated
  16
+    render :layout => "talk_from_action"
  17
+  end
  18
+
11 19
   def erb_content_for
12 20
     render :layout => "talk_from_action"
13 21
   end
@@ -49,8 +57,18 @@ def test_content_for
49 57
     assert_equal expected_content_for_output, @response.body
50 58
   end
51 59
 
  60
+  def test_should_concatentate_content_for
  61
+    get :content_for_concatenated
  62
+    assert_equal expected_content_for_output, @response.body
  63
+  end
  64
+
52 65
   def test_erb_content_for
53  
-    get :content_for
  66
+    get :erb_content_for
  67
+    assert_equal expected_content_for_output, @response.body
  68
+  end
  69
+
  70
+  def test_should_set_content_for_with_parameter
  71
+    get :content_for_with_parameter
54 72
     assert_equal expected_content_for_output, @response.body
55 73
   end
56 74
 

0 notes on commit 4156497

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