Ticket #8796: better_capture_helper_docs.diff
| File better_capture_helper_docs.diff, 5.9 kB (added by kampers, 2 years ago) |
|---|
-
actionpack/lib/action_view/helpers/capture_helper.rb
old new 3 3 # CaptureHelper exposes methods to let you extract generated markup which 4 4 # can be used in other parts of a template or layout file. 5 5 # It provides a method to capture blocks into variables through capture and 6 # a way to capture a block of codefor use in a layout through content_for.6 # a way to capture a block of markup for use in a layout through content_for. 7 7 module CaptureHelper 8 # The capture method allows you to extract a part of thetemplate into a9 # variable. You can then use this va lue anywhere in your templates or layout.8 # The capture method allows you to extract part of a template into a 9 # variable. You can then use this variable anywhere in your templates or layout. 10 10 # 11 11 # ==== Examples 12 12 # The capture method can be used in RHTML (ERb) templates... … … 22 22 # "The current timestamp is #{Time.now}." 23 23 # end 24 24 # 25 # You can then use the content as a variable anywhere else. For 26 # example: 25 # You can then use that variable anywhere else. For example: 27 26 # 28 27 # <html> 29 28 # <head><title><%= @greeting %></title></head> … … 46 45 end 47 46 end 48 47 49 # Calling content_for stores theblock of markup in an identifier for later use.50 # You can make subsequent calls to the stored content in another template or inthe layout51 # by calling it by name with<tt>yield</tt>.48 # Calling content_for stores a block of markup in an identifier for later use. 49 # You can make subsequent calls to the stored content in other templates or the layout 50 # by passing the identifier as an argument to <tt>yield</tt>. 52 51 # 53 52 # ==== Examples 54 53 # 55 # <% content_for ("authorized")do %>56 # alert('You are not authorized forthat!')54 # <% content_for :not_authorized do %> 55 # alert('You are not authorized to do that!') 57 56 # <% end %> 58 57 # 59 # You can then use <tt>yield : authorized</tt> anywhere in your templates.58 # You can then use <tt>yield :not_authorized</tt> anywhere in your templates. 60 59 # 61 # <%= yield : authorized if current_user == nil%>60 # <%= yield :not_authorized if current_user.nil? %> 62 61 # 63 # You can also use th ese variablesin a layout. For example:62 # You can also use this syntax alongside an existing call to <tt>yield</tt> in a layout. For example: 64 63 # 65 64 # <!-- This is the layout --> 66 65 # <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> … … 73 72 # </body> 74 73 # </html> 75 74 # 76 # And now we'll create a view that has a content_for call that75 # And now, we'll create a view that has a content_for call that 77 76 # creates the <tt>script</tt> identifier. 78 77 # 79 78 # <!-- This is our view --> 80 79 # Please login! 81 80 # 82 # <% content_for ("script")do %>83 # <script type="text/javascript">alert('You are not authorized for this action!')</script>81 # <% content_for :script do %> 82 # <script type="text/javascript">alert('You are not authorized to view this page!')</script> 84 83 # <% end %> 85 84 # 86 # Then in another view you may wantto do something like this:85 # Then, in another view, you could to do something like this: 87 86 # 88 87 # <%= link_to_remote 'Logout', :action => 'logout' %> 89 88 # 90 # <% content_for ("script")do %>89 # <% content_for :script do %> 91 90 # <%= javascript_include_tag :defaults %> 92 91 # <% end %> 93 92 # 94 # That will include Prototype and Scriptaculous into the page; this technique95 # is useful if you'll only be using these scripts on a few views.93 # That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists) 94 # on the page; this technique is useful if you'll only be using these scripts in a few views. 96 95 # 97 # NOTE: Beware that content_for is ignored in caches. So you shouldn't use it98 # for elements that are going to be fragment cached.96 # Also, note that content_for concatenates the blocks it is given for a particular 97 # identifier in order. For example: 99 98 # 100 # The deprecated way of accessing a content_for block was to use a instance variable 101 # named @@content_for_#{name_of_the_content_block}@. So <tt><%= content_for('footer') %></tt> 102 # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred notation now is 99 # <% content_for :navigation do %> 100 # <li><%= link_to 'Home', :action => 'index' %></li> 101 # <% end %> 102 # 103 # <!-- Add some other content, or use a different template: --> 104 # 105 # <% content_for :navigation do %> 106 # <li><%= link_to 'Login', :action => 'login' %></li> 107 # <% end %> 108 # 109 # Then, in another template or layout, this code would render both links in order: 110 # 111 # <ul><%= yield :navigation %></ul> 112 # 113 # WARNING: content_for is ignored in caches. So you shouldn't use it 114 # for elements that will be fragment cached. 115 # 116 # The deprecated way of accessing a content_for block is to use an instance variable 117 # named <tt>@content_for_#{name_of_the_content_block}</tt>. So <tt><%= content_for :footer %></tt> 118 # would be avaiable as <tt><%= @content_for_footer %></tt>. The preferred usage is now 103 119 # <tt><%= yield :footer %></tt>. 104 120 def content_for(name, content = nil, &block) 105 121 eval "@content_for_#{name} = (@content_for_#{name} || '') + capture(&block)"