module ActionView::Helpers::CaptureHelper

def capture(*args)



<%= @greeting %>

<%= @greeting %>


You can then use that variable anywhere else. For example:

end
"The current timestamp is #{Time.now}."
@timestamp = capture do

...and Builder (RXML) templates.

<% end %>
<%= Time.now %>
Welcome to my shiny new web page! The date and time is
<% @greeting = capture do %>

The capture method can be used in ERb templates...
==== Examples

variable. You can then use this variable anywhere in your templates or layout.
The capture method allows you to extract part of a template into a
def capture(*args)
  value = nil
  buffer = with_output_buffer { value = yield(*args) }
  if string = buffer.presence || value and string.is_a?(String)
    NonConcattingString.new(ERB::Util.html_escape(string))
  end
end

def content_for(name, content = nil, &block)

for elements that will be fragment cached.
WARNING: content_for is ignored in caches. So you shouldn't use it

<% content_for :script, javascript_include_tag(:defaults) %>

Lastly, simple content can be passed as a parameter:

    <%= content_for :navigation %>


Then, in another template or layout, this code would render both links in order:

<% end %>
  • <%= link_to 'Login', :action => 'login' %>

  • <% content_for :navigation do %>

    <%# Add some other content, or use a different template: %>

    <% end %>
  • <%= link_to 'Home', :action => 'index' %>

  • <% content_for :navigation do %>

    identifier in order. For example:
    Note that content_for concatenates the blocks it is given for a particular

    on the page; this technique is useful if you'll only be using these scripts in a few views.
    That will place script tags for Prototype, Scriptaculous, and application.js (if it exists)

    <% end %>
    <%= javascript_include_tag :defaults %>
    <% content_for :script do %>

    <%= link_to 'Logout', :action => 'logout', :remote => true %>

    Then, in another view, you could to do something like this:

    <% end %>

    <% content_for :script do %>

    Please login!
    <%# This is our view %>

    creates the script identifier.
    And now, we'll create a view that has a content_for call that



    <%= yield %>


    <%= yield :script %>
    My Website


    <%# This is the layout %>

    You can use the yield syntax alongside an existing call to yield in a layout. For example:

    <%= stored_content %>

    This helper works just like normal helpers.

    end
    end
    content_for(:storage) || "Your storage is empty"
    def stored_content
    module StorageHelper

    content_for, however, can also be used in helper modules.

    <%= yield :not_authorized if current_user.nil? %>

    This is equivalent to:

    <%= content_for :not_authorized if current_user.nil? %>

    You can then use content_for :not_authorized anywhere in your templates.

    <% end %>
    alert('You are not authorized to do that!')
    <% content_for :not_authorized do %>

    ==== Examples

    yield doesn't work in helper modules, while content_for does.
    Note: yield can still be used to retrieve the stored content, but calling

    or the layout by passing the identifier as an argument to content_for.
    You can make subsequent calls to the stored content in other templates, helper modules
    Calling content_for stores a block of markup in an identifier for later use.
    def content_for(name, content = nil, &block)
      content = capture(&block) if block_given?
      @_content_for[name] << content if content
      @_content_for[name] unless content
    end

    def content_for?(name)



    <%= yield :right_col %>
    <%= yield %>


    <%= yield :script %>
    My Website


    <%# This is the layout %>

    Perhaps you will use different css in you layout if no content_for :right_column

    ==== Examples

    Useful to render parts of your layout differently based on what is in your views.
    content_for? simply checks whether any content has been captured yet using content_for
    def content_for?(name)
      @_content_for[name].present?
    end

    def flush_output_buffer #:nodoc:

    :nodoc:
    Add the output buffer to the response body and start a new one.
    def flush_output_buffer #:nodoc:
      if output_buffer && !output_buffer.empty?
        response.body_parts << output_buffer
        self.output_buffer = output_buffer.respond_to?(:clone_empty) ? output_buffer.clone_empty : output_buffer[0, 0]
        nil
      end
    end

    def with_output_buffer(buf = nil) #:nodoc:

    :nodoc:
    Defaults to a new empty string.
    Use an alternate output buffer for the duration of the block.
    def with_output_buffer(buf = nil) #:nodoc:
      unless buf
        buf = ActionView::OutputBuffer.new
        buf.force_encoding(output_buffer.encoding) if output_buffer.respond_to?(:encoding) && buf.respond_to?(:force_encoding)
      end
      self.output_buffer, old_buffer = buf, output_buffer
      yield
      output_buffer
    ensure
      self.output_buffer = old_buffer
    end