Hey all, I thought I''d throw this out to the Rails community and see what you guys thought of it. I''ve written what I suppose could be called a layout router that will automagically choose what layout to use based on the module being used. Here''s the code: layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match (controller.class.to_s)[1].downcase rescue ''default''} This line goes in at the top of the ApplicationController class. It will pull out the module name and use it to set the layout. If there is no module name it will default to (creatively enough) ''default.'' This can be changed to nil if no layout is desired. Please give me any feedback you have on this. Thanks, --Jeff
On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote:> Hey all, > > I thought I''d throw this out to the Rails community and see what you > guys thought of it. I''ve written what I suppose could be called a > layout router that will automagically choose what layout to use based > on the module being used. Here''s the code: > > layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match > (controller.class.to_s)[1].downcase rescue ''default''} > > This line goes in at the top of the ApplicationController class. > It will pull out the module name and use it to set the layout. If > there is no module name it will default to (creatively enough) > ''default.'' This can be changed to nil if no layout is desired. > > Please give me any feedback you have on this. > > Thanks, > --Jeff > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Hi Jeff, What do you mean by module name? Controller name? Controllers are classes, not modules, so I''m a bit confused. Also, Rails automatically looks for a layout called admin.rhtml for a controller called AdminController. If you don''t want to use it, add a layout statement or delete the file. What does the above do differently? Thanks, Nick. -- Nicholas Van Weerdenburg
Nick, When generating a controller you have the option of putting it in a ''module'' ie generate module/controller For a site that I''m working on I have 3 distinct sections (admin, store, library) with more to come. Each is very much part of the main app, but works differently from the other sections. Also each requires a different layout. So I''ve separated them into modules ie Admin::SettingsController, Store::ProductsController, Library::ArticlesController. This router will pull out admin, library, and store and use the respective layout. Mostly I wrote it because I''m lazy and don''t want to have to remember to set the layout every time I add a new module to the app. --Jeff On Nov 7, 2005, at 11:30 AM, Nicholas Van Weerdenburg wrote:> On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > >> Hey all, >> >> I thought I''d throw this out to the Rails community and see what you >> guys thought of it. I''ve written what I suppose could be called a >> layout router that will automagically choose what layout to use based >> on the module being used. Here''s the code: >> >> layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match >> (controller.class.to_s)[1].downcase rescue ''default''} >> >> This line goes in at the top of the ApplicationController class. >> It will pull out the module name and use it to set the layout. If >> there is no module name it will default to (creatively enough) >> ''default.'' This can be changed to nil if no layout is desired. >> >> Please give me any feedback you have on this. >> >> Thanks, >> --Jeff >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > Hi Jeff, > > What do you mean by module name? Controller name? Controllers are > classes, not modules, so I''m a bit confused. > > Also, Rails automatically looks for a layout called admin.rhtml for a > controller called AdminController. If you don''t want to use it, add a > layout statement or delete the file. What does the above do > differently? > > Thanks, > Nick. > -- > Nicholas Van Weerdenburg > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
A simpler way is to extend from different base classes :) and uses the default mechanism to locate layouts. Less code to maintain :) On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote:> Nick, > > When generating a controller you have the option of putting it in a > ''module'' ie generate module/controller > For a site that I''m working on I have 3 distinct sections (admin, > store, library) with more to come. Each is very much part of the main > app, but works differently from the other sections. Also each > requires a different layout. So I''ve separated them into modules ie > Admin::SettingsController, Store::ProductsController, > Library::ArticlesController. This router will pull out admin, > library, and store and use the respective layout. Mostly I wrote it > because I''m lazy and don''t want to have to remember to set the layout > every time I add a new module to the app. > > --Jeff > > On Nov 7, 2005, at 11:30 AM, Nicholas Van Weerdenburg wrote: > > > On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > > > >> Hey all, > >> > >> I thought I''d throw this out to the Rails community and see what you > >> guys thought of it. I''ve written what I suppose could be called a > >> layout router that will automagically choose what layout to use based > >> on the module being used. Here''s the code: > >> > >> layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match > >> (controller.class.to_s)[1].downcase rescue ''default''} > >> > >> This line goes in at the top of the ApplicationController class. > >> It will pull out the module name and use it to set the layout. If > >> there is no module name it will default to (creatively enough) > >> ''default.'' This can be changed to nil if no layout is desired. > >> > >> Please give me any feedback you have on this. > >> > >> Thanks, > >> --Jeff > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > > Hi Jeff, > > > > What do you mean by module name? Controller name? Controllers are > > classes, not modules, so I''m a bit confused. > > > > Also, Rails automatically looks for a layout called admin.rhtml for a > > controller called AdminController. If you don''t want to use it, add a > > layout statement or delete the file. What does the above do > > differently? > > > > Thanks, > > Nick. > > -- > > Nicholas Van Weerdenburg > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I don''t think I understand default layouts. If I were to do the following where would I put the layout for it? Library::ArticlesController < Library::BaseController I tried layouts/library/base.rhtml but it didn''t find it there --Jeff On Nov 7, 2005, at 2:32 PM, Cuong Tran wrote:> A simpler way is to extend from different base classes :) and uses the > default mechanism to locate layouts. Less code to maintain :) > > On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > >> Nick, >> >> When generating a controller you have the option of putting it in a >> ''module'' ie generate module/controller >> For a site that I''m working on I have 3 distinct sections (admin, >> store, library) with more to come. Each is very much part of the main >> app, but works differently from the other sections. Also each >> requires a different layout. So I''ve separated them into modules ie >> Admin::SettingsController, Store::ProductsController, >> Library::ArticlesController. This router will pull out admin, >> library, and store and use the respective layout. Mostly I wrote it >> because I''m lazy and don''t want to have to remember to set the layout >> every time I add a new module to the app. >> >> --Jeff >> >> On Nov 7, 2005, at 11:30 AM, Nicholas Van Weerdenburg wrote: >> >> >>> On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: >>> >>> >>>> Hey all, >>>> >>>> I thought I''d throw this out to the Rails community and see what >>>> you >>>> guys thought of it. I''ve written what I suppose could be called a >>>> layout router that will automagically choose what layout to use >>>> based >>>> on the module being used. Here''s the code: >>>> >>>> layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match >>>> (controller.class.to_s)[1].downcase rescue ''default''} >>>> >>>> This line goes in at the top of the ApplicationController class. >>>> It will pull out the module name and use it to set the layout. If >>>> there is no module name it will default to (creatively enough) >>>> ''default.'' This can be changed to nil if no layout is desired. >>>> >>>> Please give me any feedback you have on this. >>>> >>>> Thanks, >>>> --Jeff >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>> Hi Jeff, >>> >>> What do you mean by module name? Controller name? Controllers are >>> classes, not modules, so I''m a bit confused. >>> >>> Also, Rails automatically looks for a layout called admin.rhtml >>> for a >>> controller called AdminController. If you don''t want to use it, >>> add a >>> layout statement or delete the file. What does the above do >>> differently? >>> >>> Thanks, >>> Nick. >>> -- >>> Nicholas Van Weerdenburg >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
It''s based solely on the controller name (up the inheritance chain). So in your case, it will look for articles.rhtml, then base.rhtml (inside layouts). On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote:> I don''t think I understand default layouts. If I were to do the > following where would I put the layout for it? > > Library::ArticlesController < Library::BaseController > > I tried layouts/library/base.rhtml but it didn''t find it there > > --Jeff > > On Nov 7, 2005, at 2:32 PM, Cuong Tran wrote: > > > A simpler way is to extend from different base classes :) and uses the > > default mechanism to locate layouts. Less code to maintain :) > > > > On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > > > >> Nick, > >> > >> When generating a controller you have the option of putting it in a > >> ''module'' ie generate module/controller > >> For a site that I''m working on I have 3 distinct sections (admin, > >> store, library) with more to come. Each is very much part of the main > >> app, but works differently from the other sections. Also each > >> requires a different layout. So I''ve separated them into modules ie > >> Admin::SettingsController, Store::ProductsController, > >> Library::ArticlesController. This router will pull out admin, > >> library, and store and use the respective layout. Mostly I wrote it > >> because I''m lazy and don''t want to have to remember to set the layout > >> every time I add a new module to the app. > >> > >> --Jeff > >> > >> On Nov 7, 2005, at 11:30 AM, Nicholas Van Weerdenburg wrote: > >> > >> > >>> On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > >>> > >>> > >>>> Hey all, > >>>> > >>>> I thought I''d throw this out to the Rails community and see what > >>>> you > >>>> guys thought of it. I''ve written what I suppose could be called a > >>>> layout router that will automagically choose what layout to use > >>>> based > >>>> on the module being used. Here''s the code: > >>>> > >>>> layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match > >>>> (controller.class.to_s)[1].downcase rescue ''default''} > >>>> > >>>> This line goes in at the top of the ApplicationController class. > >>>> It will pull out the module name and use it to set the layout. If > >>>> there is no module name it will default to (creatively enough) > >>>> ''default.'' This can be changed to nil if no layout is desired. > >>>> > >>>> Please give me any feedback you have on this. > >>>> > >>>> Thanks, > >>>> --Jeff > >>>> _______________________________________________ > >>>> Rails mailing list > >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>>> > >>>> > >>>> > >>> Hi Jeff, > >>> > >>> What do you mean by module name? Controller name? Controllers are > >>> classes, not modules, so I''m a bit confused. > >>> > >>> Also, Rails automatically looks for a layout called admin.rhtml > >>> for a > >>> controller called AdminController. If you don''t want to use it, > >>> add a > >>> layout statement or delete the file. What does the above do > >>> differently? > >>> > >>> Thanks, > >>> Nick. > >>> -- > >>> Nicholas Van Weerdenburg > >>> _______________________________________________ > >>> Rails mailing list > >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >>> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
ah! thanks for clearing that up. On Nov 7, 2005, at 9:31 PM, Cuong Tran wrote:> It''s based solely on the controller name (up the inheritance chain). > So in your case, it will look for articles.rhtml, then base.rhtml > (inside layouts). > > On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: > >> I don''t think I understand default layouts. If I were to do the >> following where would I put the layout for it? >> >> Library::ArticlesController < Library::BaseController >> >> I tried layouts/library/base.rhtml but it didn''t find it there >> >> --Jeff >> >> On Nov 7, 2005, at 2:32 PM, Cuong Tran wrote: >> >> >>> A simpler way is to extend from different base classes :) and >>> uses the >>> default mechanism to locate layouts. Less code to maintain :) >>> >>> On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: >>> >>> >>>> Nick, >>>> >>>> When generating a controller you have the option of putting it in a >>>> ''module'' ie generate module/controller >>>> For a site that I''m working on I have 3 distinct sections (admin, >>>> store, library) with more to come. Each is very much part of the >>>> main >>>> app, but works differently from the other sections. Also each >>>> requires a different layout. So I''ve separated them into modules ie >>>> Admin::SettingsController, Store::ProductsController, >>>> Library::ArticlesController. This router will pull out admin, >>>> library, and store and use the respective layout. Mostly I wrote it >>>> because I''m lazy and don''t want to have to remember to set the >>>> layout >>>> every time I add a new module to the app. >>>> >>>> --Jeff >>>> >>>> On Nov 7, 2005, at 11:30 AM, Nicholas Van Weerdenburg wrote: >>>> >>>> >>>> >>>>> On 11/7/05, Jeff Smick <rails_lists-m0qWdWgHGwnKl/7hFL4KYti2O/JbrIOy@public.gmane.org> wrote: >>>>> >>>>> >>>>> >>>>>> Hey all, >>>>>> >>>>>> I thought I''d throw this out to the Rails community and see what >>>>>> you >>>>>> guys thought of it. I''ve written what I suppose could be called a >>>>>> layout router that will automagically choose what layout to use >>>>>> based >>>>>> on the module being used. Here''s the code: >>>>>> >>>>>> layout proc {|controller| /(?:Controllers::)?([a-zA-Z]+)::/.match >>>>>> (controller.class.to_s)[1].downcase rescue ''default''} >>>>>> >>>>>> This line goes in at the top of the ApplicationController class. >>>>>> It will pull out the module name and use it to set the layout. If >>>>>> there is no module name it will default to (creatively enough) >>>>>> ''default.'' This can be changed to nil if no layout is desired. >>>>>> >>>>>> Please give me any feedback you have on this. >>>>>> >>>>>> Thanks, >>>>>> --Jeff >>>>>> _______________________________________________ >>>>>> Rails mailing list >>>>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>>>> >>>>>> >>>>>> >>>>>> >>>>> Hi Jeff, >>>>> >>>>> What do you mean by module name? Controller name? Controllers are >>>>> classes, not modules, so I''m a bit confused. >>>>> >>>>> Also, Rails automatically looks for a layout called admin.rhtml >>>>> for a >>>>> controller called AdminController. If you don''t want to use it, >>>>> add a >>>>> layout statement or delete the file. What does the above do >>>>> differently? >>>>> >>>>> Thanks, >>>>> Nick. >>>>> -- >>>>> Nicholas Van Weerdenburg >>>>> _______________________________________________ >>>>> Rails mailing list >>>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>>> >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >