From aaf320c3c153260c6d1453c010da8bf52f2e6224 Mon Sep 17 00:00:00 2001 From: Darragh Curran Date: Fri, 31 Oct 2008 17:29:37 +0000 Subject: [PATCH] add content_for? to CaptureHelper - if layout behaves differently when a particular content_for :name exists --- .../lib/action_view/helpers/capture_helper.rb | 21 ++++++++++++++++++++ actionpack/test/controller/capture_test.rb | 11 ++++++++++ 2 files changed, 32 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index e86ca27..4b58ad0 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -122,6 +122,27 @@ module ActionView nil 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) + instance_variable_get("@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 = '') #:nodoc: diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index 5ded6a5..06701aa 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -23,6 +23,17 @@ class CaptureController < ActionController::Base def rescue_action(e) raise end end +class CaptureHelperTest < ActionView::TestCase + tests ActionView::Helpers::CaptureHelper + + def test_content_for? + assert ! content_for?(:title) + content_for :title, 'title' + assert content_for?(:title) + assert ! content_for?(:something_else) + end +end + class CaptureTest < Test::Unit::TestCase def setup @controller = CaptureController.new -- 1.5.5.1