Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 8b99df826c3b6cf56a1caaae5f931d50 > files > 751

ruby-actionpack-2.3.4-1mdv2010.0.noarch.rpm

<?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>&nbsp;&nbsp;
      <a href="#M000564">content_for</a>&nbsp;&nbsp;
      </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, &amp;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&#8230;
</p>
<pre>
  &lt;% @greeting = capture do %&gt;
    Welcome to my shiny new web page!  The date and time is
    &lt;%= Time.now %&gt;
  &lt;% end %&gt;
</pre>
<p>
&#8230;and Builder (RXML) templates.
</p>
<pre>
  @timestamp = capture do
    &quot;The current timestamp is #{Time.now}.&quot;
  end
</pre>
<p>
You can then use that variable anywhere else. For example:
</p>
<pre>
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;&lt;%= @greeting %&gt;&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;b&gt;&lt;%= @greeting %&gt;&lt;/b&gt;
  &lt;/body&gt;&lt;/html&gt;
</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, &amp;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>
  &lt;% content_for :not_authorized do %&gt;
    alert('You are not authorized to do that!')
  &lt;% end %&gt;
</pre>
<p>
You can then use <tt>yield :not_authorized</tt> anywhere in your templates.
</p>
<pre>
  &lt;%= yield :not_authorized if current_user.nil? %&gt;
</pre>
<p>
You can also use this syntax alongside an existing call to <tt>yield</tt>
in a layout. For example:
</p>
<pre>
  &lt;%# This is the layout %&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;My Website&lt;/title&gt;
    &lt;%= yield :script %&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;%= yield %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
</pre>
<p>
And now, we&#8216;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>
  &lt;%# This is our view %&gt;
  Please login!

  &lt;% content_for :script do %&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;alert('You are not authorized to view this page!')&lt;/script&gt;
  &lt;% end %&gt;
</pre>
<p>
Then, in another view, you could to do something like this:
</p>
<pre>
  &lt;%= link_to_remote 'Logout', :action =&gt; 'logout' %&gt;

  &lt;% content_for :script do %&gt;
    &lt;%= javascript_include_tag :defaults %&gt;
  &lt;% end %&gt;
</pre>
<p>
That will place &lt;script&gt; tags for Prototype, Scriptaculous, and
application.js (if it exists) on the page; this technique is useful if
you&#8216;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>
  &lt;% content_for :navigation do %&gt;
    &lt;li&gt;&lt;%= link_to 'Home', :action =&gt; 'index' %&gt;&lt;/li&gt;
  &lt;% end %&gt;

  &lt;%#  Add some other content, or use a different template: %&gt;

  &lt;% content_for :navigation do %&gt;
    &lt;li&gt;&lt;%= link_to 'Login', :action =&gt; 'login' %&gt;&lt;/li&gt;
  &lt;% end %&gt;
</pre>
<p>
Then, in another template or layout, this code would render both links in
order:
</p>
<pre>
  &lt;ul&gt;&lt;%= yield :navigation %&gt;&lt;/ul&gt;
</pre>
<p>
Lastly, simple content can be passed as a parameter:
</p>
<pre>
  &lt;% content_for :script, javascript_include_tag(:defaults) %&gt;
</pre>
<p>
WARNING: <a href="CaptureHelper.html#M000564">content_for</a> is ignored in
caches. So you shouldn&#8216;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>&lt;%= yield :footer %&gt;</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>