From 9cb8c812f2a23ab5653a7888740a014a02c97c18 Mon Sep 17 00:00:00 2001 From: Darragh Curran Date: Sun, 21 Jun 2009 17:05:43 +0100 Subject: [PATCH] Add content_for?(:name) helper to check if content_for(:name) is present [#1311 state:resolved] Signed-off-by: Pratik Naik --- .../lib/action_view/helpers/capture_helper.rb | 22 +++++++++++++++++++ .../test/template/capture_helper_test.rb | 15 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 actionpack/test/template/capture_helper_test.rb diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index a8b5a9dbb9ae9..c90acc5ac2096 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -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 %> + # + # + # My Website + # <%= yield :script %> + # + # + # <%= yield %> + # <%= yield :right_col %> + # + # + 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 0000000000000..2017a18806fd0 --- /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