HTML Title

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 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 ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

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

On 4/27/06, Anthony R. [email protected] 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.

On 4/27/06, Ray B. [email protected] wrote:

controller, but if you don’t want to set it for each controller action,


Rails mailing list
[email protected]
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.

On 4/27/06, Matt R. [email protected] 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 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

James L. wrote:

On 4/27/06, Matt R. [email protected] 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