I''ve got a layout (header/menu/content) used by one controller in my application. It looks like this: <html> <head> <title>Ideas: <%= controller.action_name %></title> <%= stylesheet_link_tag ''scaffold'' %> </head> <body> <div id="ideaBanner"> <%= render :partial => "ideas/banner" %> </div> <div id="ideaMenuAndContent"> <div id="ideaMenu"> <%= render :partial => "ideas/menu" %> </div> <div id="ideaContent"> <p style="color: green"><%= flash[:notice] %></p> <%= @content_for_layout %> </div> </div> </body> </html> Now I''m adding login functionality and I want the exact same layout and content for the login page, but I want a different menu. The only way I can see to do that is to copy the entire layout and change the render :partial for the menu to render the appropriate menu. But of course if I do that, then I''d be repeating myself. Is there a best practice for this sort of thing? TIA, david geary
Why not? <%= render :partial => @menu %> I''ve never tried it but I dont see why it wouldnt work... then you can just set @menu in the controller as needed perhaps in the ApplicationController you could set a default and only override where needed. David Geary wrote:> I''ve got a layout (header/menu/content) used by one controller in my > application. It looks like this: > > <html> > <head> > <title>Ideas: <%= controller.action_name %></title> > <%= stylesheet_link_tag ''scaffold'' %> > </head> > > <body> > <div id="ideaBanner"> > <%= render :partial => "ideas/banner" %> > </div> > > <div id="ideaMenuAndContent"> > <div id="ideaMenu"> > <%= render :partial => "ideas/menu" %> > </div> > > <div id="ideaContent"> > <p style="color: green"><%= flash[:notice] %></p> > <%= @content_for_layout %> > </div> > </div> > </body> > </html> > > Now I''m adding login functionality and I want the exact same layout > and content for the login page, but I want a different menu. The only > way I can see to do that is to copy the entire layout and change the > render :partial for the menu to render the appropriate menu. But of > course if I do that, then I''d be repeating myself. > > Is there a best practice for this sort of thing? > > TIA, > > > david geary > _______________________________________________ > 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
On Jul 23, 2005, at 5:13 PM, David Geary wrote:> I''ve got a layout (header/menu/content) used by one controller in > my application. It looks like this: > > <html> > <head> > <title>Ideas: <%= controller.action_name %></title> > <%= stylesheet_link_tag ''scaffold'' %> > </head> > > <body> > <div id="ideaBanner"> > <%= render :partial => "ideas/banner" %> > </div> > > <div id="ideaMenuAndContent"> > <div id="ideaMenu"> > <%= render :partial => "ideas/menu" %> > </div> > > <div id="ideaContent"> > <p style="color: green"><%= flash[:notice] %></p> > <%= @content_for_layout %> > </div> > </div> > </body> > </html> > > Now I''m adding login functionality and I want the exact same layout > and content for the login page, but I want a different menu. The > only way I can see to do that is to copy the entire layout and > change the render :partial for the menu to render the appropriate > menu. But of course if I do that, then I''d be repeating myself. > > Is there a best practice for this sort of thing?Helpers are useful for this sort of thing. Just define a helper "knows" what to do for the menu based on (e.g.) the controller used: <%= render_menu %> And then: def render_menu case controller.controller_name when "login" render :partial => "login/menu" else render :partial => "ideas/menu" end end With some attention to convention, you could even do away with the helper and just do: <%= render :partial => "#{controller.controller_name}/menu" %> - Jamis
Le 05-07-23 à 17:29, Jamis Buck a écrit :> On Jul 23, 2005, at 5:13 PM, David Geary wrote: > > >> I''ve got a layout (header/menu/content) used by one controller in >> my application. It looks like this: >> >> <html> >> <head> >> <title>Ideas: <%= controller.action_name %></title> >> <%= stylesheet_link_tag ''scaffold'' %> >> </head> >> >> <body> >> <div id="ideaBanner"> >> <%= render :partial => "ideas/banner" %> >> </div> >> >> <div id="ideaMenuAndContent"> >> <div id="ideaMenu"> >> <%= render :partial => "ideas/menu" %> >> </div> >> >> <div id="ideaContent"> >> <p style="color: green"><%= flash[:notice] %></p> >> <%= @content_for_layout %> >> </div> >> </div> >> </body> >> </html> >> >> Now I''m adding login functionality and I want the exact same >> layout and content for the login page, but I want a different >> menu. The only way I can see to do that is to copy the entire >> layout and change the render :partial for the menu to render the >> appropriate menu. But of course if I do that, then I''d be >> repeating myself. >> >> Is there a best practice for this sort of thing? >> > > Helpers are useful for this sort of thing. Just define a helper > "knows" what to do for the menu based on (e.g.) the controller used: > > <%= render_menu %> > > And then: > > def render_menu > case controller.controller_name > when "login" > render :partial => "login/menu" > else > render :partial => "ideas/menu" > end > endYuck.> With some attention to convention, you could even do away with the > helper and just do: > > <%= render :partial => "#{controller.controller_name}/menu" %>Yippie! Thanks, Jamis! Nowadays, I dig convention and that works very nicely. And now I''m no longer repeating myself. Thanks, Jamis! Nowadays, I dig convention and that works very nicely. And now I''m no longer repeating myself. david geary> > - Jamis > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I also wrote a module to handle this sort of thing... as long as you are changing the menu on a per-controller basis... if not.. it might be more work than its worth... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails