Ticket #3287: capture_helper_works_for_non_erb.patch
| File capture_helper_works_for_non_erb.patch, 2.6 kB (added by Brian Takita, 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 content_for_non_erb_block 12 render :layout => "talk_from_action" 13 end 10 14 11 15 def rescue_action(e) raise end 12 16 end … … 36 40 get :content_for 37 41 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 38 42 end 43 44 def test_content_for_non_erb_block 45 get :content_for_non_erb_block 46 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 47 end 39 48 40 49 def test_update_element_with_capture 41 50 get :update_element_with_capture -
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 block.call(*args) 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 66 81 # Content_for will store the given block