Titlezilla

Ultimate solution for dealing with titles in Rails.

Installation

Add this line to your application's Gemfile:

gem 'titlezilla'

And then execute:

$ bundle

Run the generator:

rails generate titlezilla:install

Usage

Basics

Put your titles in generated .yml file:

en:
  titles:
    application: My Application
    welcome:
      index: Welcome page title
    users:
      show: User: %{user}

Use convenient methods in your helpers:

# Assuming you are on welcome#index page:
application_title # => My Application
title # => Welcome page title
meta_title # => Welcome page title | My Application
title_tag # => <title>Welcome page title | My Application</title>

Passing variables to translations

All defined instance variables passed to translations.

YAML:

en:
  titles:
    users:
      show: User: %{user}

Model:

class User < ActiveRecord::Base
  def to_s
    user.full_name
  end
end

Controller:

def show
  @user = User.find(params[:id])

View:

title # => User: John Doe

Namespacing

Namespaced controllers/views are supported, so you can have separate set of titles for different parts of your application.

YAML:

en:
  titles:
    application: My Application
    ...
    admin:
      application: Admin Panel
      dashboard:
        index: Dashboard

View:

# Assuming you are on admin/welcome#index page:
application_title # => Admin Panel
title # => Dashboard
meta_title # => Dashboard | Admin Panel

Action aliasing (:create and :update)

Titleziila uses name of the current action for resolving titles, so when you submit a form and it fails to save, your rendered new template inside the create action. To avoid setting duplicated titles for both new and create (and edit and update), this actions is mapped in gem configuration. If your application have similar non-RESTful pair of actions, your can add them to mapping:

config/initializers/titlezilla.rb:

Titlezilla.configure do |config|
  config.action_map.merge!({perform_parse: :parse})
end

Now you can define title only for parse action, perform_parse whil use it automagically.

Custom meta title separator

If you want your title tag content to look like this: My Application > My cool page, tell Titlezilla to use custom separator.

config/initializers/titlezilla.rb:

Titlezilla.configure do |config|
  config.separator = ' > '
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request