From 3d1b51b4411ffe8de92d997b824637f9eaf47bb1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 26 Feb 2006 19:47:50 +0000 Subject: [PATCH] Added .rxml (and any non-rhtml template, really) supportfor CaptureHelper#content_for and CaptureHelper#capture #3287 [Brian Takita] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3669 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 + .../lib/action_view/helpers/capture_helper.rb | 61 +++++++++++++++---- actionpack/test/controller/capture_test.rb | 34 ++++++++++- .../fixtures/test/block_content_for.rhtml | 2 + .../test/fixtures/test/erb_content_for.rhtml | 2 + .../test/non_erb_block_content_for.rxml | 4 ++ 6 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 actionpack/test/fixtures/test/block_content_for.rhtml create mode 100644 actionpack/test/fixtures/test/erb_content_for.rhtml create mode 100644 actionpack/test/fixtures/test/non_erb_block_content_for.rxml diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e804343f9f..f167b6f453 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added .rxml (and any non-rhtml template, really) supportfor CaptureHelper#content_for and CaptureHelper#capture #3287 [Brian Takita] + * Added script.aculo.us drag and drop helpers to RJS [Thomas Fuchs]. Examples: page.draggable 'product-1' diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 5c1f32a96c..9828fe0fa2 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -43,24 +43,30 @@ module CaptureHelper # instance variable. You can use this instance variable anywhere # in your templates and even in your layout. # - # Example: + # Example of capture being used in a .rhtml page: # # <% @greeting = capture do %> # Welcome To my shiny new web page! - # <% end %> + # <% end %> + # + # Example of capture being used in a .rxml page: + # + # @greeting = capture do + # 'Welcome To my shiny new web page!' + # end def capture(*args, &block) # execute the block - buffer = eval("_erbout", block.binding) - pos = buffer.length - block.call(*args) + begin + buffer = eval("_erbout", block.binding) + rescue + buffer = nil + end - # extract the block - data = buffer[pos..-1] - - # replace it in the original with empty string - buffer[pos..-1] = '' - - data + if buffer.nil? + capture_block(*args, &block) + else + capture_erb_with_buffer(buffer, *args, &block) + end end # Content_for will store the given block @@ -84,6 +90,37 @@ def capture(*args, &block) def content_for(name, &block) eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)" end + + private + def capture_block(*args, &block) + block.call(*args) + end + + def capture_erb(*args, &block) + buffer = eval("_erbout", block.binding) + capture_erb_with_buffer(buffer, *args, &block) + end + + def capture_erb_with_buffer(buffer, *args, &block) + pos = buffer.length + block.call(*args) + + # extract the block + data = buffer[pos..-1] + + # replace it in the original with empty string + buffer[pos..-1] = '' + + data + end + + def erb_content_for(name, &block) + eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_erb(&block)" + end + + def block_content_for(name, &block) + eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)" + end end end end diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index 461777e4e2..e4f76a4dbf 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -7,6 +7,18 @@ def self.controller_path; "test"; end def content_for render :layout => "talk_from_action" end + + def erb_content_for + render :layout => "talk_from_action" + end + + def block_content_for + render :layout => "talk_from_action" + end + + def non_erb_block_content_for + render :layout => "talk_from_action" + end def rescue_action(e) raise end end @@ -34,7 +46,22 @@ def test_simple_capture def test_content_for get :content_for - assert_equal "Putting stuff in the title!\n\nGreat stuff!", @response.body + assert_equal expected_content_for_output, @response.body + end + + def test_erb_content_for + get :content_for + assert_equal expected_content_for_output, @response.body + end + + def test_block_content_for + get :block_content_for + assert_equal expected_content_for_output, @response.body + end + + def test_non_erb_block_content_for + get :non_erb_block_content_for + assert_equal expected_content_for_output, @response.body end def test_update_element_with_capture @@ -45,4 +72,9 @@ def test_update_element_with_capture @response.body.strip ) end + + private + def expected_content_for_output + "Putting stuff in the title!\n\nGreat stuff!" + end end diff --git a/actionpack/test/fixtures/test/block_content_for.rhtml b/actionpack/test/fixtures/test/block_content_for.rhtml new file mode 100644 index 0000000000..9510337365 --- /dev/null +++ b/actionpack/test/fixtures/test/block_content_for.rhtml @@ -0,0 +1,2 @@ +<% block_content_for :title do 'Putting stuff in the title!' end %> +Great stuff! \ No newline at end of file diff --git a/actionpack/test/fixtures/test/erb_content_for.rhtml b/actionpack/test/fixtures/test/erb_content_for.rhtml new file mode 100644 index 0000000000..c3bdd13643 --- /dev/null +++ b/actionpack/test/fixtures/test/erb_content_for.rhtml @@ -0,0 +1,2 @@ +<% erb_content_for :title do %>Putting stuff in the title!<% end %> +Great stuff! \ No newline at end of file diff --git a/actionpack/test/fixtures/test/non_erb_block_content_for.rxml b/actionpack/test/fixtures/test/non_erb_block_content_for.rxml new file mode 100644 index 0000000000..6ff6db0f95 --- /dev/null +++ b/actionpack/test/fixtures/test/non_erb_block_content_for.rxml @@ -0,0 +1,4 @@ +content_for :title do + 'Putting stuff in the title!' +end +xml << "\nGreat stuff!" \ No newline at end of file -- GitLab