Ticket #3287: capture_helper_works_for_non_erb.4.patch
| File capture_helper_works_for_non_erb.4.patch, 5.0 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 erb_content_for 12 render :layout => "talk_from_action" 13 end 14 15 def block_content_for 16 render :layout => "talk_from_action" 17 end 18 19 def non_erb_block_content_for 20 render :layout => "talk_from_action" 21 end 10 22 11 23 def rescue_action(e) raise end 12 24 end … … 34 46 35 47 def test_content_for 36 48 get :content_for 37 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body49 assert_equal expected_content_for_output, @response.body 38 50 end 51 52 def test_erb_content_for 53 get :content_for 54 assert_equal expected_content_for_output, @response.body 55 end 56 57 def test_block_content_for 58 get :block_content_for 59 assert_equal expected_content_for_output, @response.body 60 end 61 62 def test_non_erb_block_content_for 63 get :non_erb_block_content_for 64 assert_equal expected_content_for_output, @response.body 65 end 39 66 40 67 def test_update_element_with_capture 41 68 get :update_element_with_capture … … 45 72 @response.body.strip 46 73 ) 47 74 end 75 76 private 77 def expected_content_for_output 78 "<title>Putting stuff in the title!</title>\n\nGreat stuff!" 79 end 48 80 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/erb_content_for.rhtml
old new 1 <% erb_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 59 begin 60 buffer = eval("_erbout", block.binding) 61 rescue 62 buffer = nil 63 end 64 65 if buffer.nil? 66 capture_block(*args, &block) 67 else 68 capture_erb_with_buffer(buffer, *args, &block) 69 end 70 end 71 72 def capture_erb(*args, &block) 53 73 buffer = eval("_erbout", block.binding) 74 75 capture_erb_with_buffer(buffer, *args, &block) 76 end 77 78 def capture_erb_with_buffer(buffer, *args, &block) 54 79 pos = buffer.length 55 80 block.call(*args) 56 81 … … 63 88 data 64 89 end 65 90 91 def capture_block(*args, &block) 92 block.call(*args) 93 end 94 66 95 # Content_for will store the given block 67 96 # in an instance variable for later use in another template 68 97 # or in the layout. … … 84 113 def content_for(name, &block) 85 114 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)" 86 115 end 116 117 def erb_content_for(name, &block) 118 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_erb(&block)" 119 end 120 121 def block_content_for(name, &block) 122 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)" 123 end 87 124 end 88 125 end 89 126 end