# HTML Title

**URL:** <https://www.ruby-forum.com/t/html-title/94284>\
**Category:** Rails\
**Created:** [April 27, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/html-title/94284 "2006-04-27T20:30:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/html-title/94284/1 "2006-04-27T20:30:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/html-title/94284/2 "2006-04-27T20:46:36Z")

</div>

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](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/html-title/94284/3 "2006-04-27T22:11:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/html-title/94284/4 "2006-04-27T20:53:11Z")

</div>

On 4/27/06, Anthony R. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/html-title/94284/5 "2006-04-27T22:18:47Z")

</div>

On 4/27/06, Ray B. [removed\_email\_address@domain.invalid](mailto: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](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.

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/html-title/94284/6 "2006-04-27T22:59:11Z")

</div>

On 4/27/06, Matt R. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 27, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/html-title/94284/7 "2006-04-27T23:08:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anthonyrudgick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c37758/32.png) [@anthonyrudgick](https://www.ruby-forum.com/u/anthonyrudgick)\
**Post date:** [April 28, 2006, 2:57am UTC](https://www.ruby-forum.com/t/html-title/94284/8 "2006-04-28T02:57:46Z")

</div>

James L. wrote:

> On 4/27/06, Matt R. [removed\_email\_address@domain.invalid](mailto: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
