views:

3875

answers:

9

What is the best way to create a custom title for pages in a rails app with out using a plug-in?

+6  A: 

Look into content_for: http://railscasts.com/episodes/8

Avdi
+4  A: 

Without further details on the use-case or requirements that you're trying to satisfy, I can think of several alternatives:

1) Switch the title in one of your layout pages and consume a helper method stored in application_helper.rb

<title><%= custom_title %></title>

This approach will give you a unique title for each layout page.

2) Railscasts suggests using a partial to load what shows up between the HEAD tags

3) Use javascript/ajax calls to manipulate the DOM if you need to change the title after the load event.

Maybe you don't really want to change the content tagged by the title element. Perhaps you really need a breadcrumb of some sort, so that your users always know where they are with respect to your site's navigation hierarchy. While I've done fine with how the goldberg plugin, I'm sure there are other ways of pulling off the same functionality.

Alan
+13  A: 

Best practice is to use content_for.

First, add a couple of helper methods (ie. stick in app/helpers/application_helper.rb):

def page_title
  (@content_for_title + " &mdash; " if @content_for_title).to_s + 'My Cool Site'
end

def page_heading(text)
  content_tag(:h1, content_for(:title){ text })
end

@content_for_title is basically like using yield(:title), except you'll run into problems when trying to call yield like that from a helper method.

Then in your layout view you can simply use:

<title><%= page_title %></title>

...and in the view itself:

<%= page_heading "Awesome" %>

This way has the advantage of allowing you to shuffle where you stick the h1 tag for your title, and keeps your controller nice and free of pesky @title variables.

Aupajo
+1  A: 

You can also set it in a before_filter in your controller.

# foo_controller.rb

class FooController < ApplicationController

  before_filter :set_title

  private

  def set_title
    @page_title = "Foo Page"
  end

end

# application.html.erb

<h1><%= page_title %></h1>

You can then set conditions in the set_title method to set a different titles for different actions in the controller. It's nice to be able to see all the relevant page titles within your controller.

JasonOng
It's (unfortunately) common practice to use this way, but it's bad practice to put page titles in your controller. A page title is something more related to the view. If you were to offer an XML alternative view for instance, what relevance does a page title have?
Aupajo
Yeah I agree that it's not quite a good idea but however it's an alternative that might be useful in some cases where different page titles needs to be birthed from complex conditions?
JasonOng
Hmm, I still think it's something that belongs as a helper for the view. My rule is to look at the XML version and see if it needs that variable or not. If not, it's something that an individual view needs. Make your controller as light as possible.
Aupajo
+20  A: 

In your views do something like this:

<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%=h content_for(:title, "Title for specific page") %></h1>

The following goes in the layout file:

<head>
  <title><%=h yield(:title) %></title>
  <!-- Additional header tags here -->
</head>
<body>
  <!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
  <h1><%=h yield(:title) %></h1>
</body>

It's also possible to encapsulate the content_for and yield(:title) statements in helper methods (as others have already suggested). However, in simple cases such as this one I like to put the necessary code directly into the specific views without custom helpers.

Christoph Schiessl
A: 

I would like to add my pretty simple variant.

In the ApplicationController define this method:

  def get_title
    @action_title_name || case controller_name
                            when 'djs'
                              'Djs'
                            when 'photos'
                              'Photos'
                            when 'events'
                              'Various events'
                            when 'static'
                              'Info'
                            when 'club'
                              'My club'
                            when 'news'
                              'News'
                            when 'welcome'
                              'Welcome!'
                            else
                              'Other'
                          end
  end

After that you can call get_title from your layout's title tag. You can define more specific title for your page by defining @action_title_name variable in your actions.

IDBD
A: 

You can use the title-tag!

f.e.:

<html>
<head>
  <title>
    Christoph this is a good answer!
  </title>
</header>
<body>
</body>
</html>

Look for it under: http://de.selfhtml.org/html/kopfdaten/titel.htm

p.s.: You dont have to thank me! Just check my answer as the correct one...

Lichtamberg
A: 

Here's a simple option that I like to use

In your layout

<head>
  <title><%= @title %></title>
</head>

And at the top of your page template (first line)

<% @title="Home" %>

Because of the way the layout and page templates are parsed the @title="Home" is evaluated before the layout is rendered.

opsb
+2  A: 

I use nifty_generator's "nifty_layout" which provides with a title variable which I can call then on the page using:

<% title "Title of page" %>

I can also user <% title "Title of page", false %> to have the title just show in browser title and not in the page itself.

Senthil