neilang / implicit_page_titles

Smart page title defaults for your Rails app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

implicit_page_titles gem

Allows you to easily set page titles per view in your Rails 4 app, but also follows Ruby on Rails naming conventions to set sensible default title if none is specified.

Example

Route Page title
/ Application name
/login "Login"
/posts "Posts"
/posts/:id post.title
/author/:id author.name
/posts/:id/new "New Post"
/posts/:id/edit "Edit #{post.title}"

Usage

In your layout add the page_title helper method.

e.g. within app/views/layouts/application.html.erb

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

Explicitly setting a page title

To set a specific title for a view, call the page_title method within the view with desired title.

e.g. within app/views/welcome/index.html.erb

  <%- page_title("My page title") %>

This method will also return the set title, enabling syntax like this:

  <h1><%= page_title("My page title") %></h1>

Adding extra information to the page title

It is best to do this within the title tag:

  <title>
    <% unless current_page?(root_url) %>
      My app:
    <% end %>
    <%= page_title %>
  </title>

Compatibility

Only tested with Rails 4+ and Ruby 2+. Although untested, it would likely work with Rails 3 and Ruby 1.9.x.

License

This project rocks and uses MIT-LICENSE.

About

Smart page title defaults for your Rails app

License:MIT License


Languages

Language:Ruby 78.9%Language:HTML 17.8%Language:CSS 1.7%Language:JavaScript 1.5%