Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple content_for on the same page
    primarykey
    data
    text
    <p>I have large block of HTML in my application that I would like to move into a shared template and then use content_for with yields to insert the necessary content. However, if I use it more than once in the same layout file the content_for just appends to the previous making that idea not work so well. Is there a solution to this?</p> <pre><code>&lt;div class="block side"&gt; &lt;div class="block_head"&gt; &lt;div class="bheadl"&gt;&lt;/div&gt; &lt;div class="bheadr"&gt;&lt;/div&gt; &lt;h2&gt;&lt;%= yield :block_head %&gt;&lt;/h2&gt; &lt;/div&gt; &lt;div class="block_content"&gt; &lt;%= yield :block_content %&gt; &lt;/div&gt; &lt;div class="bendl"&gt;&lt;/div&gt; &lt;div class="bendr"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>and I use the following code to set the content for the block</p> <pre><code> &lt;%= overwrite_content_for :block_head do -%&gt; My Block &lt;% end -%&gt; &lt;%= overwrite_content_for :block_content do -%&gt; &lt;p&gt;My Block Content&lt;/p&gt; &lt;% end -%&gt; &lt;%= render :file =&gt; "shared/_blockside" %&gt; </code></pre> <p>The problem is if I use this multiple times on the same layout the content from the original block is appended to the secondary block</p> <p>I have tried creating a custom helper method to get around it, however it does not return any content</p> <pre><code> def overwrite_content_for(name, content = nil, &amp;block) @_content_for[name] = "" content_for(name, content &amp;block) end </code></pre> <p>I may also be going about this completely wrong and if there are any better methods for getting content to work like this I would like to know. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload