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

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)

Add the erb_content_for, capture_erb, and capture_erb_with_buffer methods.

  • actionpack/test/controller/capture_test.rb

    old new  
    77  def content_for 
    88    render :layout => "talk_from_action" 
    99  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 
    1022 
    1123  def rescue_action(e) raise end 
    1224end 
     
    3446   
    3547  def test_content_for 
    3648    get :content_for 
    37     assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!", @response.body 
     49    assert_equal expected_content_for_output, @response.body 
    3850  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 
    3966 
    4067  def test_update_element_with_capture 
    4168    get :update_element_with_capture 
     
    4572      @response.body.strip 
    4673    ) 
    4774  end 
     75   
     76  private 
     77  def expected_content_for_output 
     78    "<title>Putting stuff in the title!</title>\n\nGreat stuff!" 
     79  end 
    4880end 
  • 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/erb_content_for.rhtml

    old new  
     1<% erb_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 
     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) 
    5373        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) 
    5479        pos = buffer.length 
    5580        block.call(*args) 
    5681         
     
    6388        data 
    6489      end 
    6590       
     91      def capture_block(*args, &block) 
     92        block.call(*args) 
     93      end 
     94       
    6695      # Content_for will store the given block 
    6796      # in an instance variable for later use in another template 
    6897      # or in the layout.  
     
    84113      def content_for(name, &block) 
    85114        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)" 
    86115      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 
    87124    end 
    88125  end 
    89126end