Skip to content
This repository
Browse code

Add content_for?(:name) helper to check if content_for(:name) is pres…

…ent [#1311 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 9cb8c812f2a23ab5653a7888740a014a02c97c18 1 parent 66eb058
Darragh Curran authored June 21, 2009 lifo committed June 21, 2009
22  actionpack/lib/action_view/helpers/capture_helper.rb
@@ -117,6 +117,28 @@ def content_for(name, content = nil, &block)
117 117
         @_content_for[name]
118 118
       end
119 119
 
  120
+      # content_for? simply checks whether any content has been captured yet using content_for
  121
+      # Useful to render parts of your layout differently based on what is in your views.
  122
+      # 
  123
+      # ==== Examples
  124
+      #
  125
+      # Perhaps you will use different css in you layout if no content_for :right_column
  126
+      #
  127
+      #   <%# This is the layout %>
  128
+      #   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  129
+      #   <head>
  130
+      #	    <title>My Website</title>
  131
+      #	    <%= yield :script %>
  132
+      #   </head>
  133
+      #   <body class="<%= content_for?(:right_col) ? 'one-column' : 'two-column' %>">
  134
+      #     <%= yield %>
  135
+      #     <%= yield :right_col %>
  136
+      #   </body>
  137
+      #   </html>
  138
+      def content_for?(name)
  139
+        @_content_for[name].present?
  140
+      end
  141
+
120 142
       # Use an alternate output buffer for the duration of the block.
121 143
       # Defaults to a new empty string.
122 144
       def with_output_buffer(buf = nil) #:nodoc:
15  actionpack/test/template/capture_helper_test.rb
... ...
@@ -0,0 +1,15 @@
  1
+require 'abstract_unit'
  2
+
  3
+class CaptureHelperTest < ActionView::TestCase
  4
+  def setup
  5
+    super
  6
+    @_content_for = Hash.new {|h,k| h[k] = "" }
  7
+  end
  8
+
  9
+  def test_content_for
  10
+    assert ! content_for?(:title)
  11
+    content_for :title, 'title'
  12
+    assert content_for?(:title)
  13
+    assert ! content_for?(:something_else)
  14
+  end
  15
+end

0 notes on commit 9cb8c81

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