diff options
author | Darragh Curran <darragh@peelmeagrape.net> | 2009-06-21 17:05:43 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-06-21 17:07:22 +0100 |
commit | 9cb8c812f2a23ab5653a7888740a014a02c97c18 (patch) | |
tree | ed207d0ec3e013b1921a1c8951ba46d51fa86967 | |
parent | 66eb05821b1cb522f497c874e7708fe705fb8356 (diff) | |
download | rails-9cb8c812f2a23ab5653a7888740a014a02c97c18.tar.gz rails-9cb8c812f2a23ab5653a7888740a014a02c97c18.tar.bz2 rails-9cb8c812f2a23ab5653a7888740a014a02c97c18.zip |
Add content_for?(:name) helper to check if content_for(:name) is present [#1311 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r-- | actionpack/lib/action_view/helpers/capture_helper.rb | 22 | ||||
-rw-r--r-- | actionpack/test/template/capture_helper_test.rb | 15 |
2 files changed, 37 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index a8b5a9dbb9..c90acc5ac2 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -117,6 +117,28 @@ module ActionView @_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: diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb new file mode 100644 index 0000000000..2017a18806 --- /dev/null +++ b/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 |