Git Product home page Git Product logo

dub_thee's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.