<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActionView::Helpers::CaptureHelper</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActionView::Helpers::CaptureHelper</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/action_view/helpers/capture_helper_rb.html"> lib/action_view/helpers/capture_helper.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> <a href="CaptureHelper.html">CaptureHelper</a> exposes methods to let you extract generated markup which can be used in other parts of a template or layout file. It provides a method to <a href="CaptureHelper.html#M000563">capture</a> blocks into variables through <a href="CaptureHelper.html#M000563">capture</a> and a way to <a href="CaptureHelper.html#M000563">capture</a> a block of markup for use in a layout through <a href="CaptureHelper.html#M000564">content_for</a>. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000563">capture</a> <a href="#M000564">content_for</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000563" class="method-detail"> <a name="M000563"></a> <div class="method-heading"> <a href="CaptureHelper.src/M000563.html" target="Code" class="method-signature" onclick="popupCode('CaptureHelper.src/M000563.html');return false;"> <span class="method-name">capture</span><span class="method-args">(*args, &block)</span> </a> </div> <div class="method-description"> <p> The <a href="CaptureHelper.html#M000563">capture</a> method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout. </p> <h4>Examples</h4> <p> The <a href="CaptureHelper.html#M000563">capture</a> method can be used in ERb templates… </p> <pre> <% @greeting = capture do %> Welcome to my shiny new web page! The date and time is <%= Time.now %> <% end %> </pre> <p> …and Builder (RXML) templates. </p> <pre> @timestamp = capture do "The current timestamp is #{Time.now}." end </pre> <p> You can then use that variable anywhere else. For example: </p> <pre> <html> <head><title><%= @greeting %></title></head> <body> <b><%= @greeting %></b> </body></html> </pre> </div> </div> <div id="method-M000564" class="method-detail"> <a name="M000564"></a> <div class="method-heading"> <a href="CaptureHelper.src/M000564.html" target="Code" class="method-signature" onclick="popupCode('CaptureHelper.src/M000564.html');return false;"> <span class="method-name">content_for</span><span class="method-args">(name, content = nil, &block)</span> </a> </div> <div class="method-description"> <p> Calling <a href="CaptureHelper.html#M000564">content_for</a> stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to <tt>yield</tt>. </p> <h4>Examples</h4> <pre> <% content_for :not_authorized do %> alert('You are not authorized to do that!') <% end %> </pre> <p> You can then use <tt>yield :not_authorized</tt> anywhere in your templates. </p> <pre> <%= yield :not_authorized if current_user.nil? %> </pre> <p> You can also use this syntax alongside an existing call to <tt>yield</tt> in a layout. For example: </p> <pre> <%# This is the layout %> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>My Website</title> <%= yield :script %> </head> <body> <%= yield %> </body> </html> </pre> <p> And now, we‘ll create a view that has a <a href="CaptureHelper.html#M000564">content_for</a> call that creates the <tt>script</tt> identifier. </p> <pre> <%# This is our view %> Please login! <% content_for :script do %> <script type="text/javascript">alert('You are not authorized to view this page!')</script> <% end %> </pre> <p> Then, in another view, you could to do something like this: </p> <pre> <%= link_to_remote 'Logout', :action => 'logout' %> <% content_for :script do %> <%= javascript_include_tag :defaults %> <% end %> </pre> <p> That will place <script> tags for Prototype, Scriptaculous, and application.js (if it exists) on the page; this technique is useful if you‘ll only be using these scripts in a few views. </p> <p> Note that <a href="CaptureHelper.html#M000564">content_for</a> concatenates the blocks it is given for a particular identifier in order. For example: </p> <pre> <% content_for :navigation do %> <li><%= link_to 'Home', :action => 'index' %></li> <% end %> <%# Add some other content, or use a different template: %> <% content_for :navigation do %> <li><%= link_to 'Login', :action => 'login' %></li> <% end %> </pre> <p> Then, in another template or layout, this code would render both links in order: </p> <pre> <ul><%= yield :navigation %></ul> </pre> <p> Lastly, simple content can be passed as a parameter: </p> <pre> <% content_for :script, javascript_include_tag(:defaults) %> </pre> <p> WARNING: <a href="CaptureHelper.html#M000564">content_for</a> is ignored in caches. So you shouldn‘t use it for elements that will be fragment cached. </p> <p> The deprecated way of accessing a <a href="CaptureHelper.html#M000564">content_for</a> block is to use an instance variable named <tt>@content_for_#{name_of_the_content_block}</tt>. The preferred usage is now <tt><%= yield :footer %></tt>. </p> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>