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:

#layout

.review[review]
.thumb
= yield :thumb
.details
%h4
= yield :heading
%div
Rest of layout…

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

index.html.haml

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

  • if section == :thumb
    thumb stuff

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

index.html.haml

- @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), @product.name)
}

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…

end

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

Tomorrow if there is need to change the other partial view it would be
clean
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs