Hi there, If I have an application.rhtml template what''s the best way to set the contents of title element in the html -> head area of the template to something set in the view for action in a controller. I want to just have one template which renders the basic layout for all pages and I''ve been scratching my head over this one for a couple of hours now. Any help gratefully received. Thanks Ant
Hi -- On Thu, 27 Apr 2006, Anthony Ramm wrote:> Hi there, > > If I have an application.rhtml template what''s the best way to set the > contents of title element in the html -> head area of the template to > something set in the view for action in a controller. I want to just have > one template which renders the basic layout for all pages and I''ve been > scratching my head over this one for a couple of hours now.Put it in an instance variable: def my_action @page_title = "This page''s title" and in the layout: <html> <head> <title><%= @page_title %> David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!
On 4/27/06, Anthony Ramm <anthony@openadvantage.org> wrote:> > If I have an application.rhtml template what''s the best way to set > the contents of title element in the html -> head area of the > template to something set in the view for action in a controller. >Anthony, Any instance variable you set in your view will be available in your layout, e.g. --- view.rhtml --- <% @page_title = ''foo'' %> <p>This is my view.</p> --- layout.rhtml --- <html> <head> <title><%= @page_title || ''default'' %></title> </head> <body> <%= yield %> </body> </html> ~ j. // Hope this helps. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060427/34498f93/attachment.html
Anthony Ramm wrote:> If I have an application.rhtml template what''s the best way to set the > contents of title element in the html -> head area of the template to > something set in the view for action in a controller. I want to just > have one template which renders the basic layout for all pages and I''ve > been scratching my head over this one for a couple of hours now.You have two good answers if you want to set it explicitly in each controller, but if you don''t want to set it for each controller action, you could do something like: <title>Site name: <%= controller.controller_name.capitalize %> <%= controller.action_name.capitalize %></title> in your application template. -- Ray
On 4/27/06, Ray Baxter <ray@warmroom.com> wrote:> > Anthony Ramm wrote: > > > If I have an application.rhtml template what''s the best way to set the > > contents of title element in the html -> head area of the template to > > something set in the view for action in a controller. I want to just > > have one template which renders the basic layout for all pages and I''ve > > been scratching my head over this one for a couple of hours now. > > You have two good answers if you want to set it explicitly in each > controller, but if you don''t want to set it for each controller action, > you could do something like: > > <title>Site name: <%= controller.controller_name.capitalize %> <%> controller.action_name.capitalize %></title> > > in your application template. > > -- > > Ray > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >I was going over this in the AWD book this afternoon. I''m not sure what page it is, but according to that, you should avoid putting <%= @page_title || ''someTitleHere'' %> in your views and keep it in a controller so you don''t have to update every view if you ever change the default title. -- Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060427/a58410ae/attachment-0001.html
On 4/27/06, Matt Ramos <sleepjunk13@gmail.com> wrote:> I was going over this in the AWD book this afternoon. I''m not sure what page > it is, but according to that, you should avoid putting <%= @page_title || > ''someTitleHere'' %> in your views and keep it in a controller so you don''t > have to update every view if you ever change the default title.Rather than using ''someTitleHere'' call a helper method that returns your default title. Then you only have to define it once.
Matt Ramos wrote:> > > On 4/27/06, *Ray Baxter* <ray@warmroom.com > <mailto:ray@warmroom.com>> wrote: > > Anthony Ramm wrote: > > > If I have an application.rhtml template what''s the best way to > set the > > contents of title element in the html -> head area of the template to > > something set in the view for action in a controller. I want to > just > > have one template which renders the basic layout for all pages > and I''ve > > been scratching my head over this one for a couple of hours now. > > You have two good answers if you want to set it explicitly in each > controller, but if you don''t want to set it for each controller action, > you could do something like: > > <title>Site name: <%= controller.controller_name.capitalize %> <%> controller.action_name.capitalize %></title> > > in your application template. > > I was going over this in the AWD book this afternoon. I''m not sure what > page it is, but according to that, you should avoid putting <%= > @page_title || ''someTitleHere'' %> in your views and keep it in a > controller so you don''t have to update every view if you ever change the > default title.Good point, although Anthony was asking, and I was answering, about putting it in application.rhtml, not in every template. I use application.rhtml as the default layout for all of my pages, so if I decide to change my title, there is only one source. -- Ray
James Ludlow wrote:> On 4/27/06, Matt Ramos <sleepjunk13@gmail.com> wrote: >> I was going over this in the AWD book this afternoon. I''m not sure what page >> it is, but according to that, you should avoid putting <%= @page_title || >> ''someTitleHere'' %> in your views and keep it in a controller so you don''t >> have to update every view if you ever change the default title. > > Rather than using ''someTitleHere'' call a helper method that returns > your default title. Then you only have to define it once.Or put it as a constant in environment.rb, e.g. DEFAULT_TITLE = "Something" and then do @page_title || DEFAULT_TITLE Keep in mind that you need to reboot the server for changes to environment.rb to go into effect. -- HenrikN -- Posted via http://www.ruby-forum.com/.