From 1c598517fba56e3e50d0ef4e6ebba5f3a152f057 Mon Sep 17 00:00:00 2001 From: Tom Lea Date: Wed, 13 Aug 2008 13:13:11 +0100 Subject: [PATCH] content_tag blocks can now take a string passed into them to which it can append generated content. --- actionpack/lib/action_view/helpers/tag_helper.rb | 9 ++++++++- actionpack/test/template/tag_helper_test.rb | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index de08672..50a0250 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -65,10 +65,17 @@ module ActionView # Hello world! # <% end -%> # # =>
Hello world!
+ # + # content_tag :div, :class => "strong" do |content| + # content << "Hello" + # content << " world!" + # end + # # =>
Hello world!
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) - content_tag = content_tag_string(name, capture(&block), options, escape) + content = block.arity == 1 ? returning("", &block) : capture(&block) + content_tag = content_tag_string(name, content, options, escape) if block_called_from_erb?(block) concat(content_tag) diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index fc49d34..6c86fe9 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -34,12 +34,23 @@ class TagHelperTest < ActionView::TestCase content_tag("a", "Create", :href => "create") end + def test_content_tag_using_block_with_parameter_out_of_erb + rv = content_tag(:div, :class => "green") {|content| content << "Hello"; content << " world!" } + assert_dom_equal %(
Hello world!
), rv + end + def test_content_tag_with_block_in_erb __in_erb_template = '' content_tag(:div) { concat "Hello world!" } assert_dom_equal "
Hello world!
", output_buffer end + def test_content_tag_using_block_with_parameter_in_erb + __in_erb_template = '' + content_tag(:div) {|content| content << "Hello"; content << " world!" } + assert_dom_equal "
Hello world!
", output_buffer + end + def test_content_tag_with_block_and_options_in_erb __in_erb_template = '' content_tag(:div, :class => "green") { concat "Hello world!" } -- 1.5.2.4