Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render rails partial multiple times on same page

I have a partial that I'm rendering twice on the same page, but in two different locations (one is shown during standard layout, one is shown during mobile/tablet layout).

The partial is rendered exactly the same in both places, so I'd like to speed it up by storing it as a variable if possible; the partial makes an API call each time, and the 2nd call is completely unnecessary since it's a duplicate of the first API call.

Is there any way to store the HTML from the returned partial as a variable and then use that for both renders?

Edit: I'm hoping to do this without caching, as it is a very simple need and I'm looking to keep the codebase lean and readable. Is it possible to store the partial as a string variable and then reference that twice?

like image 813
Tyler Avatar asked Dec 07 '22 13:12

Tyler


1 Answers

<% content_for :example do %>
  <%= render :your_partial %>
<%end%>

then call <%= yield :example %> or <%= content_for :example %> wherever you want your partial called.

like image 200
Kyle C Avatar answered Dec 21 '22 02:12

Kyle C