Is there a way to do a sub-layout? For example, you have an application level layout that does things like headers, footers, menus, etc. This works great, since these things appear on every page. However, a good number of pages require some decoration (e.g. tabs), which wrap around the unique content (what''s normally in @content_for_layout). It would be great if you could use a tab layout that indicates that it should be inserted into the application layout. Is there a way to easily do this in Rails? Thanks! Jen
render :partial => ''sub_layout'' will do what you are after. Warmest regards, Nathan. -------------------------------------------------------------- Nathaniel S. H. Brown Toll Free 1.877.4.INIMIT Inimit Innovations Phone 604.724.6624 www.inimit.com Fax 604.444.9942> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of jennyw > Sent: November 10, 2005 2:42 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Sub-layouts? > > Is there a way to do a sub-layout? For example, you have an > application level layout that does things like headers, > footers, menus, etc. This works great, since these things > appear on every page. However, a good number of pages require > some decoration (e.g. tabs), which wrap around the unique > content (what''s normally in @content_for_layout). It would be > great if you could use a tab layout that indicates that it > should be inserted into the application layout. > > Is there a way to easily do this in Rails? > > Thanks! > > Jen > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nathaniel S. H. Brown wrote:>render :partial => ''sub_layout'' will do what you are after. > >Thanks, Nathaniel. I thought of that, but couldn''t think of a way to do it that wouldn''t involve calls to two partial templates (one for before, one for after). For example, I have an application.rhtml template that creates a top menu and a left menu. Most of the time, the content does not include tabs, but many pages do include tabs. The tabs would need to wrap around my content. I could accomplish this with two partials. I''d call the first partial at the beginning of my view, and the second one at the very bottom of my view (like a header and footer). This works, but it''s not the same as a sub-layout, where I was envisioning that you could do the same thing with a single file. There might not be such a thing -- I just wanted to check and see if there was some sub-layout functionality I just wasn''t aware of. Jen
Assuming these templates: page->tabs->content I think a "render ''content''" or "render @content" tag in the ''tabs'' template would do the trick, which itself gets assigned to page''s content_for_layout. Or perhaps it''s possible to capture render content (render_component possibly) and assign that to page''s content_for_layout. csn --- jennyw <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote:> Nathaniel S. H. Brown wrote: > > >render :partial => ''sub_layout'' will do what you > are after. > > > > > Thanks, Nathaniel. I thought of that, but couldn''t > think of a way to do > it that wouldn''t involve calls to two partial > templates (one for before, > one for after). > > For example, I have an application.rhtml template > that creates a top > menu and a left menu. Most of the time, the content > does not include > tabs, but many pages do include tabs. The tabs > would need to wrap > around my content. > > I could accomplish this with two partials. I''d call > the first partial at > the beginning of my view, and the second one at the > very bottom of my > view (like a header and footer). This works, but > it''s not the same as a > sub-layout, where I was envisioning that you could > do the same thing > with a single file. There might not be such a thing > -- I just wanted to > check and see if there was some sub-layout > functionality I just wasn''t > aware of. > > Jen > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________ Yahoo! Mail - PC Magazine Editors'' Choice 2005 http://mail.yahoo.com
On 11/11/05, jennyw <jennyw-eRDYlh02QjuxE3qeFv2dE9BPR1lH4CV8@public.gmane.org> wrote:> I could accomplish this with two partials. I''d call the first partial at > the beginning of my view, and the second one at the very bottom of my > view (like a header and footer). This works, but it''s not the same as a > sub-layout, where I was envisioning that you could do the same thing > with a single file. There might not be such a thing -- I just wanted to > check and see if there was some sub-layout functionality I just wasn''t > aware of.Just did a quick test to see if I could get something working, the code is attached. Lets say you have ''app/views/layouts/main.rhtml'' and ''app/views/layouts/sub.rhtml''. In controllers that should have the sub layout, you can set the layout explicitly to ''sub'': ------ class MyController < ApplicationController layout ''sub'' end ------ Then do this in your sub layout: ------ <% content = @content_for_layout @content_for_layout = nil content_for :layout do %> This is in the sub layout and will appear _inside_ the main layout. <%= content %> <% end %> <%= render ''layouts/main'' %> ------ It''s a little cumbersome, I''ll admin -- but it works. The other option would be to keep all that logic in your actions: ------ class MyController < ApplicationController def index @content_for_layout = render_to_string :action => ''index'', :layout => ''sub'' render :template => ''layouts/main'' end end ------ If you wanted to be really ambitious, it probably wouldn''t be too hard to patch ActionController::Base so that you could do (using the concept of the last example): ------ class MyController < ApplicationController def index render :action => ''index'', :layout => [ ''main'', ''sub'' ] end end ------ or even just: ------ class MyController < ApplicationController layout [ ''main'', ''sub'' ] def index end end ------ but I''ll leave that as an exercise for the reader. ;-) -- Regards, John Wilger http://johnwilger.com ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
John Wilger wrote:>Just did a quick test to see if I could get something working, the >code is attached. > >Lots of good ideas to think about. I think I''ll eventually take up your excercise for the reader, but probably not for this project (which is due posthaste). Thanks! Jen