This project is archived and is in readonly mode.
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 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 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 August 16th, 2008 @ 08:46 PM
thanks, documentation is planned, just wanted to nail down the functionality first.
-
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 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 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 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.
-
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 October 28th, 2008 @ 08:36 PM
I was playing with that idea but it got real hairy real fast. Good Luck :)
-
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 October 29th, 2008 @ 02:23 AM
- State changed from open to resolved
O, you make it look so easy :)
-
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
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>
People watching this ticket
Attachments
Referenced by
- 847 Pass block args for render :layout (from [38c7d73e73d569211c4dfadf96fc295a925b7c9c]) pass yi...