Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Parameterized partials

#2467

Suggestion

I suggest to parameterize 'smart' partials rendering like that:


<%= render :partial => @cart.products, :context => 'cart' %>

which would be an equivalent to something like that:


<%= render :partial => 'products/product_for_cart', :collection => @cart.products %>

Justification

In the snippet below all product, category and cart pages (in a standard 'category has many products' scenario) display the same views/products/_product.html.erb partial.


# views/products/show.html.erb
<%= render @products, :locals => {:context => 'product'} %>

# views/categories/show.html.erb
<%= render @category.products, :locals => {:context => 'category'} %>

# views/shopping_cart/show.html.erb
<%= render @cart.products, :locals => {:context => 'cart'} %>

This leads to overly complicated views/products/_product.html.erb logic of what and how to display that depends on context. I found that readability is more important than DRY sometimes and I usually end up with a series of partials:


views/products/_product.html.erb
views/products/_category_product.html.erb
views/products/_cart_product.html.erb

and render them appropriately:


<%= render :partial => 'products/category_product', :collection => @category.products %>

I suggest to improve smart partials by parameterizing their rendering a-la:


<%= render :partial => @cart.products, :context => 'cart' %>

which would be an equivalent to


<%= render :partial => 'products/product_for_cart', :collection => @cart.products %>

Reported by ebelin · April 9th, 2009 @ 07:23 PM

State: invalid
Milestone: 2.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Steve St. Martin
    Steve St. Martin
    • Assigned user set to Ryan Bigg

    Not sure I 100% understand the usecase here why if readability was more important than dry how adding :context makes it anymore readable.

    I would use the following, it gives you the separation you require, no added functionality, and reads cleanly showing its intent. the only sacrifice is the partials are not all contained under views/products. However i would argue that if the partials are so heavily different they cannot be shared, they are likely tied to the section they are contained within to begin with.

      # views/shopping_cart/show.html.erb -> /views/shopping_cart/_product.html.erb
      <%= render :partial => 'product', :collection => @cart.products %>
    
      # views/categories/show.html.erb -> /views/categories/_product.html.erb
      <%= render :partial => 'product', :collection => @category.products %>
    
      # views/products/show.html.erb -> /views/products/_product.html.erb
      <%= render :partial => 'product', :collection => @products %>
    

    suggest marking as invalid

    April 16th, 2010 @ 12:28 AM

  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to invalid

    That final example you can just render @products.

    April 16th, 2010 @ 12:38 AM