If I have one <%= yield %> tag then all my views render in the same place in the layout. Can I have different <%= yield %> tags for different views? Is so how do I do this? Thanks
If I have one <%= yield %> tag then all my views render in
Share
Look into ActionView::Helpers::CaptureHelper. You can do something like this in your views:
This will run the template inside the content_for block, but will not output as part of the regular template
yieldbuffer, it will be stored in a separate buffer for later. Then later on, including in the layout, you can useyield :content_nameto output the content:So in a sense you can have different
yields for different views, you just have to give the differing content a name withcontent_forin the views, and yield it with that same name in the layout.Consider your case, where you want different views in different places. Let’s say you have three panels, panel1, panel2, and panel3. You can do this in your layout:
You don’t even need to include a plain
<%= yield %>if you don’t want to. Then in your views, you can choose which panel to display the content in by surrounding the entire view with the appropriatecontent_for. For example, one of your views might be changed like this:To show in panel 2. Another one might be intended for panel 3, like this: