forked from mokolabs/headliner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
110 lines (70 loc) · 2.38 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
=========
Headliner
=========
Headliner DRYs up your page titles.
Background
==========
Normally, if your Rails application has lots of actions and a shared
layout, you might find yourself setting custom page title names in your
controllers.
Here's an example:
class PagesController < ApplicationController
def about
@title = "About us"
end
end
Then, in your main layout, you might have something like this:
<head>
<title>My website<% if @title %>: <%= @title %><% end %></title>
</head
This works okay... but page titles don't really belong in controllers, do
they?
So, by moving these titles into your views, we can DRY things up a bit and
reinforce the MVC design pattern that's so fundamental to Ruby on Rails.
Usage
=====
First, add this code to your main layout:
<head>
<%= title :site => "My website" %>
</head>
Then, to set the page title, add this to each of your views:
<h1><%= title "My page title" %></h1>
When views are rendered, the page title will be included in the right
spots:
<head>
<title>My website | My page title</title>
</head>
<body>
<h1>My page title</h1>
</body>
Options
=======
Use these options to customize the title format:
:prefix (text between site name and separator)
:separator (text used to separate website name from page title)
:suffix (text between separator and page title)
:lowercase (when true, the page name will be lowercase)
:reverse (when true, the page and site names will be reversed)
And here are a few examples to give you ideas.
<%= title :separator => "—" %>
<%= title :prefix => false, :separator => ":" %>
<%= title :lowercase => true %>
<%= title :reverse => true, :prefix => false %>
Dealing with special pages
==========================
How do you set the page title without showing it in the view?
<% title "My page title" %>
What if your view headline is different from your page title?
<%= title "My page title", "My headline" %>
How does it work?
=================
Ruby on Rails renders actions *before* inserting them into layouts. So, if
you set a variable in your view, it will be accessible in your layout. But,
at first glance, it looks like you're using a variable (in the head)
before it's been assigned a value (in the body). Cool, huh?
Credits
=======
Special thanks to Nick Zadrozny and Jordan Fowler for their input.
Feedback
========
Comments, bug reports, and svn diffs welcome at http://the.railsi.st.