Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Adds :layout option to render :partial when a collection is given. #5310

Merged
merged 2 commits into from Mar 6, 2012

Conversation

Projects
None yet
2 participants
Contributor

Serabe commented Mar 6, 2012

Short story

Adds support for layout option when rendering a partial for a collection.

Long story

I had a view like this one:

<ul>
<% collection.each do | item | -%>
  <li>
    <%= render partial: 'a_partial', object: item %>
  </li>
<% end -%>
</ul>

But it felt wrong using an iterator with just a partial inside. Currently, for cleaning this up you would need a partial like

</li><li>

and modify the original view to use a spacer_template

<ul>
<li>
  <%= render partial: 'a_partial', object: item, spacer_template: 'li_spacer_template' %>
</li>
</ul>

But it still felt wrong since spacer template is not valid html, so I thought of using a simple layout like this one:

<li>
  <%= yield %>
</li>

When I realized that render partial has no layout option when a collection is given. This pull request adds it and now, code like this is allowed:

<ul>
  <%= render partial: 'a_partial', object: item, layout: 'li_layout' %>
</ul>

It is shorter and seems better to me.

Contributor

josevalim commented Mar 6, 2012

Looks good to me. Could you please add a CHANGELOG entry and update the documentation? Thanks!

Contributor

Serabe commented Mar 6, 2012

Done.

@josevalim josevalim added a commit that referenced this pull request Mar 6, 2012

@josevalim josevalim Merge pull request #5310 from Serabe/layout_for_partials_with_collect…
…ions

Adds :layout option to render :partial when a collection is given.
d87ec9d

@josevalim josevalim merged commit d87ec9d into rails:master Mar 6, 2012

Contributor

josevalim commented Mar 6, 2012

Great job, thanks for the patch.

Contributor

Serabe commented Mar 6, 2012

Thanks to you guys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment