module ActionView::Helpers::CaptureHelper

def content_for(name, content = nil, options = {}, &block)

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

<% 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 only the last link:

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

  • <% content_for :navigation, flush: true do %>

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

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

  • <% content_for :navigation do %>

    If the flush parameter is +true+ content_for replaces the blocks it is given. For example:

      <%= 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 %>

    And in another place:

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

  • <% content_for :navigation do %>

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

    this technique is useful if you'll only be using these scripts in a few views.
    That will place +script+ tags for your default set of JavaScript files on the page;

    <% 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 %>

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

    <%= 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 %>

    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, you would pass the identifier as an argument to content_for.
    In order to access this 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, options = {}, &block)
      if content || block_given?
        if block_given?
          options = content if content
          content = capture(&block)
        end
        if content
          options[:flush] ? @view_flow.set(name, content) : @view_flow.append(name, content)
        end
        nil
      else
        @view_flow.get(name).presence
      end
    end