#847 √ resolved
Ryan Bates

Pass block args for render :layout

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

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.

  • Joshua Peek

    Joshua Peek 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...

  • Joshua Peek

    Joshua Peek October 28th, 2008 @ 05:34 PM

    • → Milestone changed from “2.x” to “2.2”
    • → State changed from “resolved” to “open”
    • → Assigned user changed from “” to “Joshua Peek”

    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.

  • Joshua Peek

    Joshua Peek 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.

  • Joshua Peek

    Joshua Peek October 29th, 2008 @ 02:23 AM

    • → State changed from “open” to “resolved”

    O, you make it look so easy :)

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins