Changeset 4262
- Timestamp:
- 04/25/06 04:03:51 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/layout.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/templates/scaffolds/layout.rhtml (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/capture_helper.rb (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r4261 r4262 1 1 *SVN* 2 3 * Update layout and content_for documentation to use yield rather than magic @content_for instance variables. [Marcel Molina Jr.] 2 4 3 5 * 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 28 28 # 29 29 # <!-- The header part of this layout --> 30 # <%= @content_for_layout%>30 # <%= yield %> 31 31 # <!-- The footer part of this layout --> 32 32 # … … 48 48 # 49 49 # <h1><%= @page_title %></h1> 50 # <%= @content_for_layout%>50 # <%= yield %> 51 51 # 52 52 # ...and content pages that fulfill these references _at_ rendering time: … … 160 160 # As you can see, you pass the template as the first parameter, the status code as the second ("200" is OK), and the layout 161 161 # 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. 162 165 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 166 168 # performance and have access to them as any normal template would. 167 169 def layout(template_name, conditions = {}) trunk/actionpack/lib/action_controller/templates/scaffolds/layout.rhtml
r3416 r4262 64 64 <p style="color: green"><%= flash[:notice] %></p> 65 65 66 <%= @content_for_layout%>66 <%= yield %> 67 67 68 68 </body> trunk/actionpack/lib/action_view/helpers/capture_helper.rb
r3669 r4262 1 1 module ActionView 2 2 module Helpers 3 # Capture lets you extract parts of code into instance variableswhich3 # Capture lets you extract parts of code which 4 4 # can be used in other points of the template or even layout file. 5 5 # … … 9 9 # [some html...] 10 10 # <% end %> 11 #12 11 # 13 12 # == Add javascript to header using content_for 14 13 # 15 # content_for("name") is a wrapper for capture which will store the16 # 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. 17 16 # 18 17 # layout.rhtml: … … 22 21 # <title>layout with js</title> 23 22 # <script type="text/javascript"> 24 # <%= @content_for_script %>23 # <%= yield :script %> 25 24 # </script> 26 25 # </head> 27 26 # <body> 28 # <%= @content_for_layout%>27 # <%= yield %> 29 28 # </body> 30 29 # </html> … … 70 69 end 71 70 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. 79 74 # 80 75 # Example: … … 84 79 # <% end %> 85 80 # 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 %> 87 84 # 88 85 # 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>. 90 92 def content_for(name, &block) 91 93 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"