This project is archived and is in readonly mode.

#847 ✓resolved
Ryan Bates

Pass block args for render :layout

Reported by Ryan Bates | August 16th, 2008 @ 06:58 PM

Currently it's possible to pass a block to "render :layout" in the view and yield to it. For example:


<!-- _layout.html.erb -->
before
<%= yield %>
end

<!-- some view --->
<%= render :layout => 'layout' do %>
middle
<% end %>

<!-- outputs -->
before
middle
end

However, you can not pass any arguments to that block when you yield. The included patch adds this ability. You can then do some really cool stuff like use that as an iterator if you pass a collection to layout.


<!-- _comment.html.erb -->
before
<%= yield comment %>
after

<!-- some view -->
<% render :layout => @comments do |comment| %>
  <%= comment.created_at %>
<% end %>

<!-- output -->
before
2008-01-01 00:00:00 (whatever time the comment was created)
after

This is very useful if you need to dynamically change some part of a partial when you're rendering it.

Comments and changes to this ticket

  • Ryan Bates

    Ryan Bates August 16th, 2008 @ 07:14 PM

    Also, you could get fancy and yield multiple times for different sections of the layout. For example.

    
    <!-- _comment.html.erb -->
    before
    <%= yield comment, :time %>
    middle
    <%= yield comment, :extra %>
    after
    
    <!-- some view -->
    <% render :layout => @comments do |comment, section| %>
      <% case section when :time %>
        <%= comment.created_at %>
      <% when :extra %>
        some extra content
      <% end %>
    <% end %>
    
    <!-- output -->
    before
    2008-01-01 00:00:00 (whatever time the comment was created)
    middle
    some extra content
    after
    

    This way you can easily and dynamically customize various parts of the layout/partial when rendering it.

  • DHH

    DHH August 16th, 2008 @ 08:43 PM

    I like this, but we need to get some documentation on the method along with the patch.

  • Ryan Bates

    Ryan Bates August 16th, 2008 @ 08:46 PM

    thanks, documentation is planned, just wanted to nail down the functionality first.

  • Ryan Bates

    Ryan Bates August 16th, 2008 @ 09:15 PM

    Here's an updated patch with docs.

  • josh

    josh August 17th, 2008 @ 05:05 AM

    If @proc_for_layout is private, lets prefix it with an underscore. I started doing that with a few other instance variables that are available in the view scope.

    The execute method was always a bad smell on Base. I wonder if we could easily move that over into the Renderable#render method. (This isn't exactly related to your patch buts its always nice to cleanup these little things when we spot them)

  • Ryan Bates

    Ryan Bates August 17th, 2008 @ 05:29 AM

    Here's an updated patch with the underscore prefix. I'm not sure what's involved in moving the execute method over to renderable, so I'll leave that to you. :)

  • Repository

    Repository August 18th, 2008 @ 01:30 AM

    • State changed from “new” to “resolved”

    (from [38c7d73e73d569211c4dfadf96fc295a925b7c9c]) pass yielded arguments to block for ActionView::Base#render with :layout [#847 state:resolved]

    Signed-off-by: Joshua Peek josh@joshpeek.com http://github.com/rails/rails/co...

  • josh

    josh October 28th, 2008 @ 05:34 PM

    • Assigned user set to “josh”
    • Milestone cleared.
    • State changed from “resolved” to “open”

    Someone found a 2.1 regression bug due to this commit.

    The block arguments passed in shadow any existing @content_for_*.

    
    # index.erb
    <% content_for :column do %>column<% end %>
    <% render :layout => 'layouts/column' do %>content<% end %>
    
    # layouts/column.erb
    <div id="column"><%= yield :column %></div>
    <div id="content"><%= yield %></div>
    

    The "yield :column" always returns what is in the block "content" first and it never looks up @content_for_column.

    http://gist.github.com/20441

  • Ryan Bates

    Ryan Bates October 28th, 2008 @ 06:56 PM

    This should be fixable by checking for the existence of the @content_for_* instance variable before executing the block. I'll try to write up a patch later today.

  • josh

    josh October 28th, 2008 @ 08:36 PM

    I was playing with that idea but it got real hairy real fast. Good Luck :)

  • Ryan Bates

    Ryan Bates October 28th, 2008 @ 08:37 PM

    Here's a patch which fixes this problem by checking for the existence of the content_for ivar before executing the block. Your tests are included.

  • josh

    josh October 29th, 2008 @ 02:23 AM

    • State changed from “open” to “resolved”

    O, you make it look so easy :)

  • Brendon

    Brendon April 14th, 2009 @ 06:19 AM

    I'm experiencing a bug when trying to yield a Date object. It results in:

    @content_for_2009-03-29' is not allowed as an instance variable name

  • Ryan Bigg

    Ryan Bigg October 9th, 2010 @ 10:03 PM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 21st, 2010 @ 03:38 AM

    Automatic cleanup of spam.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages