Ticket #3287: capture_helper_works_for_non_erb.3.patch
| File capture_helper_works_for_non_erb.3.patch, 4.4 kB (added by anonymous, 3 years ago) |
|---|
-
actionpack/test/controller/capture_test.rb
old new 7 7 def content_for 8 8 render :layout => "talk_from_action" 9 9 end 10 11 def block_content_for 12 render :layout => "talk_from_action" 13 end 14 15 def non_erb_block_content_for 16 render :layout => "talk_from_action" 17 end 10 18 11 19 def rescue_action(e) raise end 12 20 end … … 34 42 35 43 def test_content_for 36 44 get :content_for 37 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body45 assert_equal expected_content_for_output, @response.body 38 46 end 47 48 def test_block_content_for 49 get :block_content_for 50 assert_equal expected_content_for_output, @response.body 51 end 52 53 def test_non_erb_block_content_for 54 get :non_erb_block_content_for 55 assert_equal expected_content_for_output, @response.body 56 end 39 57 40 58 def test_update_element_with_capture 41 59 get :update_element_with_capture … … 45 63 @response.body.strip 46 64 ) 47 65 end 66 67 private 68 def expected_content_for_output 69 "<title>Putting stuff in the title!</title>\n\nGreat stuff!" 70 end 48 71 end -
actionpack/test/fixtures/test/block_content_for.rhtml
old new 1 <% block_content_for :title do 'Putting stuff in the title!' end %> 2 Great stuff! -
actionpack/test/fixtures/test/non_erb_block_content_for.rxml
old new 1 content_for :title do 2 'Putting stuff in the title!' 3 end 4 xml << "\nGreat stuff!" -
actionpack/lib/action_view/helpers/capture_helper.rb
old new 43 43 # instance variable. You can use this instance variable anywhere 44 44 # in your templates and even in your layout. 45 45 # 46 # Example :46 # Example of capture being used in a .rhtml page: 47 47 # 48 48 # <% @greeting = capture do %> 49 49 # Welcome To my shiny new web page! 50 # <% end %> 50 # <% end %> 51 # 52 # Example of capture being used in a .rxml page: 53 # 54 # @greeting = capture do 55 # 'Welcome To my shiny new web page!' 56 # end 51 57 def capture(*args, &block) 52 58 # execute the block 53 buffer = eval("_erbout", block.binding) 54 pos = buffer.length 55 block.call(*args) 59 begin 60 buffer = eval("_erbout", block.binding) 61 rescue 62 buffer = nil 63 end 56 64 57 # extract the block 58 data = buffer[pos..-1] 59 60 # replace it in the original with empty string 61 buffer[pos..-1] = '' 62 63 data 65 if buffer.nil? 66 capture_block(*args, &block) 67 else 68 pos = buffer.length 69 block.call(*args) 70 71 # extract the block 72 data = buffer[pos..-1] 73 74 # replace it in the original with empty string 75 buffer[pos..-1] = '' 76 77 data 78 end 64 79 end 65 80 81 def capture_block(*args, &block) 82 block.call(*args) 83 end 84 66 85 # Content_for will store the given block 67 86 # in an instance variable for later use in another template 68 87 # or in the layout. … … 84 103 def content_for(name, &block) 85 104 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)" 86 105 end 106 107 def block_content_for(name, &block) 108 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)" 109 end 87 110 end 88 111 end 89 112 end