page_title
Set the page title on Rails apps.
Installation
Add this line to your application's Gemfile:
gem "page_title"
And then execute:
$ bundle
Or install it yourself as:
$ gem install page_title
Usage
You can use this on your layout file to display the page title:
<%= page_title %>
To ignore the base translation, use page_title.simple
.
<%= page_title.simple %>
The page title will be inferred from the controller and action names. For an action SiteController#index
you'll need the following translate scope:
en:
titles:
base: "%{title} • MyCompany"
site:
index: "Welcome to our site"
The titles.base
scope is required and will be used to set a general title information, like your company's name.
You can also set the page title for namespaced controllers. For an action Admin::Site#index
you'll need the following translate scope:
en:
titles:
base: "%{title} • MyCompany"
admin:
site:
index: "Welcome to our site"
Sometimes you need to render some dynamic value. In this case, you can use the I18n placeholders.
en:
titles:
base: "%{title} • MyCompany"
workshops:
show: "%{name}"
You can set dynamic values using the PageSite::Base#[]=
.
class WorkshopsController < ApplicationController
def show
@workshop = Workshop.find_by_permalink!(params[:permalink])
page_title[:name] = @workshop.name
end
end
You can also use a custom translation scope. Just set the :scope
option.
class PagesController < ApplicationController
def show
page_title[:scope] = "pages.titles.#{params[:page]}"
end
end
If you're creating a controller that do not extend from ApplicationController
, remember to use the lines below:
class AdminController < ActionController::Base
include PageTitle::Helpers
helper_method :page_title
end
We detect the following action aliases:
- Action
create
points tonew
- Action
update
points toedit
- Action
destroy
points toremove
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request