Saturday, 15 February 2014

templates - Partial with multiple yields in rails -


I'm looking for partial solutions with multiple yields

In the original example, I have this idea structure:

The original application .erb ( / views / layouts / application.erb ):

 < Code> & lt ;! DOCTYPE html & gt; & Lt; Top & gt; & Lt; Title & gt; Some title & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div class = "page" & gt; & Lt;% = yield% & gt; & Lt; / Div & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

To make my code dry, partial ( / views / shared / content.erb ):

  & lt; Div class = "content" & gt; & Lt; Div class = "sidebar" & gt; & Lt;% = yield: sidebar% & gt; & Lt; / Div & gt; & Lt; Div class = "main" & gt; & Lt;% = yield% & gt; & Lt; / Div & gt; & Lt; / Div & gt;   

and the controller view ( / views / home / index.erb ):

  & lt;% = render: partial = & Gt; 'Layout / Header'% & gt; & Lt;% = Render: Partial = & gt; 'Share / Navigation'% & gt; & Lt;% #this I% & gt; & Lt;% = Render: Layout = & gt; 'Share / Contents' & gt; & Lt;% content_for: sidebar% & gt; & Lt;% # This application will go to. ARB, not content.erb% & gt; & Lt;% = Render: Partial = & gt; 'Share / Menu'% & gt; & Lt;% end% & gt; & Lt;% = yield% & gt; & Lt;% end% & gt; & Lt;% = Render: Partial = & gt; 'Layout / Footer'% & gt;   

So the main issue here is a template block with many yielding areas and the ability to render custom HTML passes or other partial.

This question is outdated, however, even when I was looking for an answer on Google it was also relevant is.

I have a solution, while still not beautiful, works very well. This idea uses the rail ' capture method, which takes a block and stores its contents in a variable:

controller.html.

  & lt;% = Render 'Shared / Partial', Body: Capture {%> My body content & lt;%}, footer: capture {%> My footer content & lt;%}% & gt;   

share / _partial.html.erb

  & lt; Div id = "body" & gt; & Lt;% = body% & gt; & Lt; / Div & gt; & Lt; Div id = "footer" & gt; & Lt;% = Footer% & gt; & Lt; / Div & gt;   

Hope this helps someone!

No comments:

Post a Comment