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?
One option would be to use fragment caching. After you wrap the partial with a cache block, the second call should show the cached version of the first. For example:
To store the result of the render to a string, you could try the
render_to_stringmethod ofAbstractController. The arguments are the same as forrender.