Render: collection wraps every element?
I am currently using:
<% @items.each do |item| %>
<li class="list-item">
<%= render :partial => '/widgets/vertical_widget',
:object => item %>
</li>
<% end %>
to display about 20 elements on the page (there are also 20 different widgets on the same page).
When I look at the logs of my server, it shows ~ 400 ms for visualization of the widget, adding up to ~ 20 thousand ms for the page. From what I read, use: colletion instead of a loop with: object should help improve those times, but I'm not sure how I can wrap each instance of the widget in LI if I use: collection. Never place a widget on a site in a list, so it makes no sense to include LI in the widget code.
I could include the widget code directly in the loop, and not in the partial one, but I do not want the code updates to be in several places.
Any other ideas for better performance would be appreciated!
content_tag :
#some_file.html.erb
<ul>
<%= render :partial => 'widgets/vertical_widget',
:collection => @items,
:locals => { :wrap_in => :li } %>
</ul>
#/widgets/vertical_widget.html.erb
#First, render and capture the content once.
<% @rendered_content = capture do %>
#render the item here
<% end %>
#Next, decide if the content rendered above should be wrapped in a tag or not
#If the "wrap_in" variable was passed-in and it is not nil/empty, then use that
#value for the tag; else do not wrap the content in a tag
<% if defined?(wrap_in) && !wrap_in.blank? %>
<%= content_tag wrap_in do %>
<%= @rendered_content %>
<% end %>
<% else %>
<%= @rendered_content %>
<% end %>