Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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)

I added the capture_block and block_content_for methods. This will allow rhtml files to capture the return value of the block rather than the _erbout value.

  • actionpack/test/controller/capture_test.rb

    old new  
    77  def content_for 
    88    render :layout => "talk_from_action" 
    99  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 
    1018 
    1119  def rescue_action(e) raise end 
    1220end 
     
    3442   
    3543  def test_content_for 
    3644    get :content_for 
    37     assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 
     45    assert_equal expected_content_for_output, @response.body 
    3846  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 
    3957 
    4058  def test_update_element_with_capture 
    4159    get :update_element_with_capture 
     
    4563      @response.body.strip 
    4664    ) 
    4765  end 
     66   
     67  private 
     68  def expected_content_for_output 
     69    "<title>Putting stuff in the title!</title>\n\nGreat stuff!" 
     70  end 
    4871end 
  • actionpack/test/fixtures/test/block_content_for.rhtml

    old new  
     1<% block_content_for :title do 'Putting stuff in the title!' end %> 
     2Great stuff! 
  • actionpack/test/fixtures/test/non_erb_block_content_for.rxml

    old new  
     1content_for :title do 
     2  'Putting stuff in the title!' 
     3end 
     4xml << "\nGreat stuff!" 
  • actionpack/lib/action_view/helpers/capture_helper.rb

    old new  
    4343      # instance variable. You can use this instance variable anywhere 
    4444      # in your templates and even in your layout.  
    4545      #  
    46       # Example
     46      # Example of capture being used in a .rhtml page
    4747      #  
    4848      #   <% @greeting = capture do %> 
    4949      #     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 
    5157      def capture(*args, &block) 
    5258        # 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 
    5664         
    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 
    6479      end 
    6580       
     81      def capture_block(*args, &block) 
     82        block.call(*args) 
     83      end 
     84       
    6685      # Content_for will store the given block 
    6786      # in an instance variable for later use in another template 
    6887      # or in the layout.  
     
    84103      def content_for(name, &block) 
    85104        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)" 
    86105      end 
     106       
     107      def block_content_for(name, &block) 
     108        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)" 
     109      end 
    87110    end 
    88111  end 
    89112end