Forum: Ruby on Rails HTML Title

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Anthony R. (Guest)
on 2006-04-27 22:30
(Received via mailing list)
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
unknown (Guest)
on 2006-04-27 22:46
(Received via mailing list)
Hi --

On Thu, 27 Apr 2006, Anthony R. 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 (removed_email_address@domain.invalid)
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!
John B. (Guest)
on 2006-04-27 22:53
(Received via mailing list)
On 4/27/06, Anthony R. <removed_email_address@domain.invalid> 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.
Ray B. (Guest)
on 2006-04-28 00:11
(Received via mailing list)
Anthony R. 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
Matt R. (Guest)
on 2006-04-28 00:18
(Received via mailing list)
On 4/27/06, Ray B. <removed_email_address@domain.invalid> wrote:
> controller, but if you don't want to set it for each controller action,
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> 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.
James L. (Guest)
on 2006-04-28 00:59
(Received via mailing list)
On 4/27/06, Matt R. <removed_email_address@domain.invalid> 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.
Ray B. (Guest)
on 2006-04-28 01:08
(Received via mailing list)
Matt R. wrote:
>      > something set in the view for action in a controller.  I want to
>     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
Henrik N (Guest)
on 2006-04-28 04:57
James L. wrote:
> On 4/27/06, Matt R. <removed_email_address@domain.invalid> 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
This topic is locked and can not be replied to.