HTML Title


#1

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


#2

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:

<%= @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!


#3

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:

Site name: <%= controller.controller_name.capitalize %> <%= controller.action_name.capitalize %>

in your application template.

Ray


#4

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’ %>

This is my view.

— layout.rhtml —

<%= @page_title || 'default' %> <%= yield %>

~ j. // Hope this helps.


#5

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.


#6

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.


#7

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


#8

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