Skip to content

Commit

Permalink
Add content_for?(:name) helper to check if content_for(:name) is pres…
Browse files Browse the repository at this point in the history
…ent [#1311 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
darragh authored and lifo committed Jun 21, 2009
1 parent 66eb058 commit 9cb8c81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions actionpack/lib/action_view/helpers/capture_helper.rb
Expand Up @@ -117,6 +117,28 @@ def content_for(name, content = nil, &block)
@_content_for[name]
end

# content_for? simply checks whether any content has been captured yet using content_for
# Useful to render parts of your layout differently based on what is in your views.
#
# ==== Examples
#
# Perhaps you will use different css in you layout if no content_for :right_column
#
# <%# This is the layout %>
# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
# <head>
# <title>My Website</title>
# <%= yield :script %>
# </head>
# <body class="<%= content_for?(:right_col) ? 'one-column' : 'two-column' %>">
# <%= yield %>
# <%= yield :right_col %>
# </body>
# </html>
def content_for?(name)
@_content_for[name].present?
end

# Use an alternate output buffer for the duration of the block.
# Defaults to a new empty string.
def with_output_buffer(buf = nil) #:nodoc:
Expand Down
15 changes: 15 additions & 0 deletions actionpack/test/template/capture_helper_test.rb
@@ -0,0 +1,15 @@
require 'abstract_unit'

class CaptureHelperTest < ActionView::TestCase
def setup
super
@_content_for = Hash.new {|h,k| h[k] = "" }
end

def test_content_for
assert ! content_for?(:title)
content_for :title, 'title'
assert content_for?(:title)
assert ! content_for?(:something_else)
end
end

0 comments on commit 9cb8c81

Please sign in to comment.