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

Changeset 4262

Show
Ignore:
Timestamp:
04/25/06 04:03:51 (3 years ago)
Author:
marcel
Message:

Update layout and content_for documentation to use yield rather than magic @content_for instance variables.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r4261 r4262  
    11*SVN* 
     2 
     3* Update layout and content_for documentation to use yield rather than magic @content_for instance variables. [Marcel Molina Jr.] 
    24 
    35* Fix assert_redirected_to tests according to real-world usage.  Also, don't fail if you add an extra :controller option: [Rick] 
  • trunk/actionpack/lib/action_controller/layout.rb

    r3989 r4262  
    2828    # 
    2929    #   <!-- The header part of this layout --> 
    30     #   <%= @content_for_layout %> 
     30    #   <%= yield %> 
    3131    #   <!-- The footer part of this layout --> 
    3232    # 
     
    4848    # 
    4949    #   <h1><%= @page_title %></h1> 
    50     #   <%= @content_for_layout %> 
     50    #   <%= yield %> 
    5151    # 
    5252    # ...and content pages that fulfill these references _at_ rendering time: 
     
    160160    # As you can see, you pass the template as the first parameter, the status code as the second ("200" is OK), and the layout 
    161161    # as the third. 
     162    # 
     163    # NOTE: The old notation for rendering the view from a layout was to expose the magic <tt>@content_for_layout</tt> instance  
     164    # variable. The preferred notation now is to use <tt>yield</tt>, as documented above. 
    162165    module ClassMethods 
    163       # If a layout is specified, all actions rendered through render and render_action will have their result assigned  
    164       # to <tt>@content_for_layout</tt>, which can then be used by the layout to insert their contents with 
    165       # <tt><%= @content_for_layout %></tt>. This layout can itself depend on instance variables assigned during action 
     166      # If a layout is specified, all rendered actions will have their result rendered   
     167      # when the layout<tt>yield</tt>'s. This layout can itself depend on instance variables assigned during action 
    166168      # performance and have access to them as any normal template would. 
    167169      def layout(template_name, conditions = {}) 
  • trunk/actionpack/lib/action_controller/templates/scaffolds/layout.rhtml

    r3416 r4262  
    6464<p style="color: green"><%= flash[:notice] %></p> 
    6565 
    66 <%= @content_for_layout %> 
     66<%= yield %> 
    6767 
    6868</body> 
  • trunk/actionpack/lib/action_view/helpers/capture_helper.rb

    r3669 r4262  
    11module ActionView 
    22  module Helpers 
    3     # Capture lets you extract parts of code into instance variables which 
     3    # Capture lets you extract parts of code which 
    44    # can be used in other points of the template or even layout file. 
    55    # 
     
    99    #     [some html...] 
    1010    #   <% end %> 
    11     #   
    1211    # 
    1312    # == Add javascript to header using content_for 
    1413    # 
    15     # content_for("name") is a wrapper for capture which will store the  
    16     # fragment in a instance variable similar to @content_for_layout
     14    # content_for("name") is a wrapper for capture which will  
     15    # make the fragment available by name to a yielding layout or template
    1716    # 
    1817    # layout.rhtml: 
     
    2221    #       <title>layout with js</title> 
    2322    #       <script type="text/javascript"> 
    24     #       <%= @content_for_script %> 
     23    #       <%= yield :script %> 
    2524    #           </script> 
    2625    #   </head> 
    2726    #   <body> 
    28     #     <%= @content_for_layout %> 
     27    #     <%= yield %> 
    2928    #   </body> 
    3029    #   </html> 
     
    7069      end 
    7170       
    72       # Content_for will store the given block 
    73       # in an instance variable for later use in another template 
    74       # or in the layout.  
    75       #  
    76       # The name of the instance variable is content_for_<name>  
    77       # to stay consistent with @content_for_layout which is used  
    78       # by ActionView's layouts 
     71      # Calling content_for stores the block of markup for later use. 
     72      # Subsequently, you can make calls to it by name with <tt>yield</tt> 
     73      # in another template or in the layout.  
    7974      #  
    8075      # Example: 
     
    8479      #   <% end %> 
    8580      # 
    86       # You can use @content_for_header anywhere in your templates. 
     81      # You can use yield :header anywhere in your templates. 
     82      # 
     83      #   <%= yield :header %> 
    8784      # 
    8885      # NOTE: Beware that content_for is ignored in caches. So you shouldn't use it 
    89       # for elements that are going to be fragment cached.  
     86      # for elements that are going to be fragment cached. 
     87      # 
     88      # The deprecated way of accessing a content_for block was to use a instance variable 
     89      # named @content_for_#{name_of_the_content_block}. So <tt><% content_for('footer') %></tt> 
     90      # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred notation now is 
     91      # <tt><%= yield :footer %></tt>. 
    9092      def content_for(name, &block) 
    9193        eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"