Project

dub_thee

0.0
No release in over 3 years
Low commit activity in last 3 years
Rails page titles via I18n
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.9

Runtime

 Project Readme

dub_thee

Rails page titles via I18n. For example, given the following application layout:

<!-- app/views/layouts/application.html.erb -->

<html>
  <head>
    <title><%= page_title %> | My Web Site</title>
  </head>
  ...
</html>

And the following translations:

## config/locales/page_title.en.yml

en:
  page_title:
    home:
      index: "Welcome"

The "app/views/home/index.html.erb" view will automatically be titled "Welcome | My Web Site".

The page_title helper fetches the title via I18n.t, using the controller name and action name to make up the translation key. Any action name is allowed, however "create", "update", and "delete" are special-cased to instead fetch the titles of their render-on-failure counterparts: "new", "edit", and "show", respectively.

Variable interpolation

For more dynamic titles, view assignment variables are made available for interpolation. Additionally, the i18n-interpolate_nested gem is included to enable attribute interpolation. For example, given the following translations:

## config/locales/page_title.en.yml

en:
  page_title:
    products:
      show: "%{product.name} (%{product.brand})"

The value of page_title for "app/views/products/show.html.erb" will be equivalent to "#{@product[:name]} (#{@product[:brand]})".

Generic titles

Generic action titles (i.e. not controller-specific) are also supported. To allow such titles to be slightly more dynamic, two additional values are provided for interpolation: plural is the String#titleize'd name of the controller, and singular is the String#singularize'd form of plural. For example, given the following translations:

## config/locales/page_title.en.yml

en:
  page_title:
    index: "%{plural}"
    new: "New %{singular}"

The value of page_title for "app/views/part_orders/index.html.erb" will be "Part Orders", and for "app/views/locations/index.html.erb" will be "Locations". Likewise, the value of page_title for "app/views/part_orders/new.html.erb" will be "New Part Order", and for "app/views/locations/new.html.erb" will be "New Location".

Additional logic

Additional logic can be incorporated by using the @page_title variable. If @page_title is set, the page_title helper will simply return @page_title. Thus, individual views can implement more nuanced title logic. For example, given the following translations:

## config/locales/page_title.en.yml

en:
  page_title:
    users:
      show: "%{user.name}"

And the following view:

<!-- app/views/users/show.html.erb -->

<% @page_title = "Your Profile" if current_user == @user %>

...

The value of page_title in "app/views/layouts/application.html.erb" will be "Your Profile" if the current logged-in user is viewing their own profile. Else, it will be equivalent to "#{@user[:name]}".

Installation

Add the gem to your Gemfile:

$ bundle add dub_thee

And run the install generator:

$ rails generate dub_thee:install

Contributing

Run bin/test to run the tests.

License

MIT License