Sharing A Layout Among Partials

Using Rails 2.3.8. My site displays reviews. Depending on what context
the review is shown in the review’s heading and thumbnail change.

I’m trying to share a layout across several review partials. Each
partial would define a few snippets of HTML to be used in the layout.

Is this possible? I’ve tried several variations of the following:


= yield :thumb
= yield :heading
Rest of layout…

Using this renders index twice (index uses a layout too):


%h2 Product reviews
= image_tag(@product.url)
= render :layout => @reviews, :locals => { :product => @product } do

  • if section == :thumb
    thumb stuff

Using this causes content to be appended upon each iteration of the
content_for blocks:


- @reviews.each do |review|
  - content_for :heading do
  Heading stuff...
  - content_for :thumb do
  Thumb stuff...

  = render :partial => "shared/review", :object => review, :locals

=> { :product => @product }

I know I can make the blocks locals:

= render :partial => “review”, :object => review, :locals => { :product
=> @product, :heading => link_to(image_tag(@product.url),

But this gets ugly as the HTML becomes more complex. Yes, I can use
helpers, but I’d prefer to keep the somewhat larger HTML snippets in the
templates instead of doing things like:

content_tag :div, :class => dom_id(@user) do
content_tag :span, “xxxxx”

more code…


I feel its better to have different partials for different kind of

Tomorrow if there is need to change the other partial view it would be
and neat way…

Typically you’d have 1 partial for the review: views/reviews/_review. In
my case the content varies slightly based on the context/review type. I
don’t think it makes sense to have N partials per context and review
type when 85% of the layout is the same.