rails partial's layouts with named yield - why is yield block never used?
Asked Answered
B

2

12

I have a partial, with a layout:

<%= render :partial => 'home/mobile/home', :layout => 'home/mobile/page', :locals => {:page => 'abc2'}%>

The layout (page.html.erb) has yields for different blocks, such as:

<div data-role="header">
  <%= yield :header %>
</div>

However, this yield block is never used, while the main-level layout file does yield as one would expect.

Is it impossible to use named content_for/yield blocks with the layouts of partials? Are there workarounds?

I would expect inheritance-- content_for :header should first look for a yield :header in the partial's layout, and failing that, the main layout file. But this is not the case. The partial layout's yield :header is simply ignored.

Brazell answered 25/9, 2011 at 18:45 Comment(0)
L
0

The workaround would be to wrap your layout into an helper method using blocks (which should be able to yield correctly).

You may want to fil a bug about the original problem.

Lunarian answered 25/9, 2011 at 20:41 Comment(1)
think you'd have to hack this a bit to get it working with rails 3Zomba
D
7

In a situation similar to yours, I replaced the yield with a call to content_for without a block. So in your example it would be simply:

<div data-role="header">
  <%= content_for :header %>
</div>

That worked for me. That yields in partials don't trickle up as you suggest may be a feature or a bug - but that's still appear to be how it works in Rails 4.1.8, 3 years down the line :)

Detachment answered 20/11, 2014 at 19:3 Comment(1)
still the case with Rails 6!Hydrophane
L
0

The workaround would be to wrap your layout into an helper method using blocks (which should be able to yield correctly).

You may want to fil a bug about the original problem.

Lunarian answered 25/9, 2011 at 20:41 Comment(1)
think you'd have to hack this a bit to get it working with rails 3Zomba

© 2022 - 2024 — McMap. All rights reserved.