meta-tags 2.4.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.codeclimate.yml +2 -3
- data/.rubocop.yml +110 -107
- data/.travis.yml +16 -28
- data/CHANGELOG.md +27 -19
- data/Gemfile +0 -1
- data/README.md +237 -214
- data/lib/meta_tags.rb +1 -0
- data/lib/meta_tags/configuration.rb +44 -0
- data/lib/meta_tags/meta_tags_collection.rb +7 -7
- data/lib/meta_tags/renderer.rb +54 -50
- data/lib/meta_tags/text_normalizer.rb +21 -11
- data/lib/meta_tags/version.rb +1 -1
- data/meta-tags.gemspec +1 -1
- metadata +2 -2
- metadata.gz.sig +0 -0
data/.travis.yml
CHANGED
@@ -1,39 +1,27 @@
|
|
1
1
|
sudo: false
|
2
2
|
cache: bundler
|
3
3
|
|
4
|
-
rvm:
|
5
|
-
- 2.0.0
|
6
|
-
- 2.1.10
|
7
|
-
- 2.2.5
|
8
|
-
- 2.3.1
|
9
|
-
- 2.4.0
|
10
|
-
|
11
4
|
env:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
5
|
+
global:
|
6
|
+
- CC_TEST_REPORTER_ID=8792cbc0d1780830ddd3dd7eb1c25f09f386ba8f04449ad830039469406a6ab3
|
7
|
+
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
|
8
|
+
matrix:
|
9
|
+
- RAILS_VERSION=4.2.9
|
10
|
+
- RAILS_VERSION=5.0.5
|
11
|
+
- RAILS_VERSION=5.1.3
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
- rvm: 2.1.10
|
24
|
-
env: "RAILS_VERSION=5.0.0"
|
25
|
-
- rvm: 2.0.0
|
26
|
-
env: "RAILS_VERSION=5.1.0"
|
27
|
-
- rvm: 2.1.10
|
28
|
-
env: "RAILS_VERSION=5.1.0"
|
13
|
+
rvm:
|
14
|
+
- 2.2.7
|
15
|
+
- 2.3.4
|
16
|
+
- 2.4.1
|
29
17
|
|
30
18
|
notifications:
|
31
19
|
recipients:
|
32
20
|
- kpumuk@kpumuk.info
|
33
21
|
|
34
|
-
|
35
|
-
|
36
|
-
|
22
|
+
before_script:
|
23
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
24
|
+
- chmod +x ./cc-test-reporter
|
37
25
|
|
38
|
-
|
39
|
-
-
|
26
|
+
after_script:
|
27
|
+
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT --coverage-input-type clover || true; fi
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,17 @@
|
|
1
|
-
## 2.
|
1
|
+
## 2.5.0 (August 23, 2017) [☰](https://github.com/kpumuk/meta-tags/compare/v2.4.1...v2.5.0)
|
2
|
+
|
3
|
+
Features:
|
4
|
+
- Fully support [Open Graph objects](https://developers.facebook.com/docs/reference/opengraph) meta tags.
|
5
|
+
|
6
|
+
Changes:
|
7
|
+
- Dropped official support for Rails older than 4.2 and Ruby older than 2.2 (both reached their End of Life)
|
8
|
+
|
9
|
+
## 2.4.1 (May 15, 2017) [☰](https://github.com/kpumuk/meta-tags/compare/v2.4.0...v2.4.1)
|
2
10
|
|
3
11
|
Features:
|
4
12
|
- Rails 5.1 support added
|
5
13
|
|
6
|
-
## 2.4.0 (December 8, 2016)
|
14
|
+
## 2.4.0 (December 8, 2016) [☰](https://github.com/kpumuk/meta-tags/compare/v2.3.1...v2.4.0)
|
7
15
|
|
8
16
|
Features:
|
9
17
|
- Added amphtml links support
|
@@ -11,7 +19,7 @@ Features:
|
|
11
19
|
Bugfixes:
|
12
20
|
- Fixed `place` attribute meta tag generation
|
13
21
|
|
14
|
-
## 2.3.1 (September 13, 2016)
|
22
|
+
## 2.3.1 (September 13, 2016) [☰](https://github.com/kpumuk/meta-tags/compare/v2.2.0...v2.3.1)
|
15
23
|
|
16
24
|
Changes:
|
17
25
|
- Added follow meta tag support
|
@@ -21,7 +29,7 @@ Features:
|
|
21
29
|
|
22
30
|
Bugfixes:
|
23
31
|
|
24
|
-
## 2.2.0 (August 24, 2016)
|
32
|
+
## 2.2.0 (August 24, 2016) [☰](https://github.com/kpumuk/meta-tags/compare/v2.1.0...v2.2.0)
|
25
33
|
|
26
34
|
Changes:
|
27
35
|
|
@@ -40,7 +48,7 @@ Bugfixes:
|
|
40
48
|
- Fixed the issue when title was truncated in some cases, when site_title was blank
|
41
49
|
- Fixed meta tag attributes for `fb:` meta tags
|
42
50
|
|
43
|
-
## 2.1.0 (October 6, 2015)
|
51
|
+
## 2.1.0 (October 6, 2015) [☰](https://github.com/kpumuk/meta-tags/compare/v2.0.0...v2.1.0)
|
44
52
|
|
45
53
|
Changes:
|
46
54
|
|
@@ -58,7 +66,7 @@ Bugfixes
|
|
58
66
|
- Generate `<meta name=""/>` instead of `<meta property=""/>` for custom meta tags
|
59
67
|
- Double HTML escaping in meta tags
|
60
68
|
|
61
|
-
## 2.0.0 (April 15, 2014)
|
69
|
+
## 2.0.0 (April 15, 2014) [☰](https://github.com/kpumuk/meta-tags/compare/v1.6.0...v2.0.0)
|
62
70
|
|
63
71
|
Features:
|
64
72
|
|
@@ -68,7 +76,7 @@ Bugfixes:
|
|
68
76
|
|
69
77
|
- Symlink references in nested hashes include use normalized meta tag values.
|
70
78
|
|
71
|
-
## 1.6.0 (April 14, 2014)
|
79
|
+
## 1.6.0 (April 14, 2014) [☰](https://github.com/kpumuk/meta-tags/compare/v1.5.0...v1.6.0)
|
72
80
|
|
73
81
|
Features:
|
74
82
|
|
@@ -80,26 +88,26 @@ Breaking changes:
|
|
80
88
|
|
81
89
|
- Removed support of Rails older than 3.0.0 due to the bug in `Hash#deep_merge` (does not support `HashWithIndifferentAccess`)
|
82
90
|
|
83
|
-
## 1.5.0 (May 7, 2013)
|
91
|
+
## 1.5.0 (May 7, 2013) [☰](https://github.com/kpumuk/meta-tags/compare/v1.4.1...v1.5.0)
|
84
92
|
|
85
93
|
Features:
|
86
94
|
|
87
95
|
- Added "prev" and "next" links support
|
88
96
|
- Added refresh meta tag support
|
89
97
|
|
90
|
-
## 1.4.1 (March 14, 2013)
|
98
|
+
## 1.4.1 (March 14, 2013) [☰](https://github.com/kpumuk/meta-tags/compare/v1.4.0...v1.4.1)
|
91
99
|
|
92
100
|
Bugfixes:
|
93
101
|
|
94
102
|
- Added support for Hash inside of an Array
|
95
103
|
|
96
|
-
## 1.4.0 (March 14, 2013)
|
104
|
+
## 1.4.0 (March 14, 2013) [☰](https://github.com/kpumuk/meta-tags/compare/v1.3.0...v1.4.0)
|
97
105
|
|
98
106
|
Features:
|
99
107
|
|
100
108
|
- Added support of custom meta tags
|
101
109
|
|
102
|
-
## 1.3.0 (February 13, 2013)
|
110
|
+
## 1.3.0 (February 13, 2013) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.6...v1.3.0)
|
103
111
|
|
104
112
|
Features:
|
105
113
|
|
@@ -112,13 +120,13 @@ Bugfixes:
|
|
112
120
|
- Do not display title HTML tag when title is blank
|
113
121
|
- Do not display OpenGraph tags when content is empty
|
114
122
|
|
115
|
-
## 1.2.6 (March 4, 2012)
|
123
|
+
## 1.2.6 (March 4, 2012) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.5...v1.2.6)
|
116
124
|
|
117
125
|
Features:
|
118
126
|
|
119
127
|
- jQuery.pjax support via `display_title` method. Check README for details
|
120
128
|
|
121
|
-
## 1.2.5 (March 3, 2012)
|
129
|
+
## 1.2.5 (March 3, 2012) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.4...v1.2.5)
|
122
130
|
|
123
131
|
Bugfixes:
|
124
132
|
|
@@ -126,7 +134,7 @@ Bugfixes:
|
|
126
134
|
- Fixed incorrect page title when `:site` is is blank
|
127
135
|
- Normalize `:og` attribute to `:open_graph`
|
128
136
|
|
129
|
-
## 1.2.4 (April 26, 2011)
|
137
|
+
## 1.2.4 (April 26, 2011) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.3...v1.2.4)
|
130
138
|
|
131
139
|
Features:
|
132
140
|
|
@@ -137,34 +145,34 @@ Bugfixes:
|
|
137
145
|
- Fixed bug with double HTML escaping in title separator
|
138
146
|
- Allow to set meta title without a separator
|
139
147
|
|
140
|
-
## 1.2.2, 1.2.3 (June 10, 2010)
|
148
|
+
## 1.2.2, 1.2.3 (June 10, 2010) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.1...v1.2.3)
|
141
149
|
|
142
150
|
Bugfixes:
|
143
151
|
|
144
152
|
- Fixed action\_pack integration (welcome back `alias_method_chain`)
|
145
153
|
- Fixed bug when `@page_*` variables did not work
|
146
154
|
|
147
|
-
## 1.2.1 (June 2, 2010)
|
155
|
+
## 1.2.1 (June 2, 2010) [☰](https://github.com/kpumuk/meta-tags/compare/v1.2.0...v1.2.1)
|
148
156
|
|
149
157
|
Bugfixes:
|
150
158
|
|
151
159
|
- Fixed deprecation warning about `html_safe!`
|
152
160
|
|
153
|
-
## 1.2.0 (May 31, 2010)
|
161
|
+
## 1.2.0 (May 31, 2010) [☰](https://github.com/kpumuk/meta-tags/compare/v1.1.1...v1.2.0)
|
154
162
|
|
155
163
|
Bugfixes:
|
156
164
|
|
157
165
|
- Fixed bug when title is set through Array, and `:lowercase` is true
|
158
166
|
- Updated `display_meta_tags` to be compatible with rails_xss
|
159
167
|
|
160
|
-
## 1.1.1 (November 21, 2009)
|
168
|
+
## 1.1.1 (November 21, 2009) [☰](https://github.com/kpumuk/meta-tags/compare/v1.1.0...v1.1.1)
|
161
169
|
|
162
170
|
Features:
|
163
171
|
|
164
172
|
- Added support for canonical link element
|
165
173
|
- Added YARD documentation
|
166
174
|
|
167
|
-
## 1.1.0 (November 5, 2009)
|
175
|
+
## 1.1.0 (November 5, 2009) [☰](https://github.com/kpumuk/meta-tags/commits/v1.1.0)
|
168
176
|
|
169
177
|
Features:
|
170
178
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -9,10 +9,13 @@ Search Engine Optimization (SEO) plugin for Ruby on Rails applications.
|
|
9
9
|
|
10
10
|
## Ruby on Rails
|
11
11
|
|
12
|
-
MetaTags master branch
|
12
|
+
MetaTags master branch fully supports Ruby on Rails 4.2+, and is tested against all
|
13
13
|
major Rails releases up to 5.1.
|
14
14
|
|
15
|
-
Ruby versions older than 2.
|
15
|
+
Ruby versions older than 2.2.0 are no longer officially supported.
|
16
|
+
|
17
|
+
_Please note_ that we are no longer support Ruby versions older than 2.2.0 and
|
18
|
+
Ruby on Rails older than 4.2, because they [reached their End of Life](https://github.com/kpumuk/meta-tags/pull/143).
|
16
19
|
|
17
20
|
## Installation
|
18
21
|
|
@@ -33,13 +36,241 @@ preferences. In order to do that, you can create an initializer
|
|
33
36
|
|
34
37
|
```ruby
|
35
38
|
MetaTags.configure do |c|
|
36
|
-
c.title_limit
|
37
|
-
c.description_limit
|
38
|
-
c.keywords_limit
|
39
|
-
c.keywords_separator
|
39
|
+
c.title_limit = 70
|
40
|
+
c.description_limit = 160
|
41
|
+
c.keywords_limit = 255
|
42
|
+
c.keywords_separator = ', '
|
43
|
+
c.property_tags.push(
|
44
|
+
'x-hearthstone:deck',
|
45
|
+
)
|
40
46
|
end
|
41
47
|
```
|
42
48
|
|
49
|
+
By default meta tags are rendered with the key `name`. Since, some meta tags are
|
50
|
+
required to use `property` instead (like Facebook Open Graph object), MetaTags gem
|
51
|
+
allows to configure which tags to render with `property` attribute. By default
|
52
|
+
the pre-configured list includes all possible Facebook Open Graph object types, but
|
53
|
+
you can add your own in case you need it.
|
54
|
+
|
55
|
+
**Please note**: Use `c.property_tags.push` instead of `c.property_tags =`, so you
|
56
|
+
do not reset the list of default tags, which would lead to invalid Open Graph
|
57
|
+
meta tags.
|
58
|
+
|
59
|
+
## MetaTags Usage
|
60
|
+
|
61
|
+
First, add this code to your main layout:
|
62
|
+
|
63
|
+
```erb
|
64
|
+
<head>
|
65
|
+
<%= display_meta_tags site: 'My website' %>
|
66
|
+
</head>
|
67
|
+
```
|
68
|
+
|
69
|
+
Then, to set the page title, add this to each of your views (see below for other options):
|
70
|
+
|
71
|
+
```erb
|
72
|
+
<h1><%= title 'My page title' %></h1>
|
73
|
+
```
|
74
|
+
|
75
|
+
When views are rendered, the page title will be included in the right spots:
|
76
|
+
|
77
|
+
```html
|
78
|
+
<head>
|
79
|
+
<title>My website | My page title</title>
|
80
|
+
</head>
|
81
|
+
<body>
|
82
|
+
<h1>My page title</h1>
|
83
|
+
</body>
|
84
|
+
```
|
85
|
+
|
86
|
+
You can find allowed options for `display_meta_tags` method below.
|
87
|
+
|
88
|
+
### Using MetaTags in controller
|
89
|
+
|
90
|
+
You can define following instance variables:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
@page_title = 'Member Login'
|
94
|
+
@page_description = 'Member login page.'
|
95
|
+
@page_keywords = 'Site, Login, Members'
|
96
|
+
```
|
97
|
+
|
98
|
+
Also you could use `set_meta_tags` method to define all meta tags simultaneously:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
set_meta_tags title: 'Member Login',
|
102
|
+
description: 'Member login page.',
|
103
|
+
keywords: 'Site, Login, Members'
|
104
|
+
```
|
105
|
+
|
106
|
+
You can find allowed options for `set_meta_tags` method below.
|
107
|
+
|
108
|
+
### Using MetaTags in view
|
109
|
+
|
110
|
+
To set meta tags you can use following methods:
|
111
|
+
|
112
|
+
```erb
|
113
|
+
<% title 'Member Login' %>
|
114
|
+
<% description 'Member login page.' %>
|
115
|
+
<% keywords 'Member login page.' %>
|
116
|
+
<% nofollow %>
|
117
|
+
<% noindex %>
|
118
|
+
<% refresh 3 %>
|
119
|
+
```
|
120
|
+
|
121
|
+
Also there is `set_meta_tags` method exists:
|
122
|
+
|
123
|
+
```erb
|
124
|
+
<% set_meta_tags title: 'Member Login',
|
125
|
+
description: 'Member login page.',
|
126
|
+
keywords: 'Site, Login, Members' %>
|
127
|
+
```
|
128
|
+
|
129
|
+
The `title` method returns title itself, so you can use it to show the title
|
130
|
+
somewhere on the page:
|
131
|
+
|
132
|
+
```erb
|
133
|
+
<h1><%= title 'Member Login' %></h1>
|
134
|
+
```
|
135
|
+
|
136
|
+
If you want to set the title and display another text, use this:
|
137
|
+
|
138
|
+
```erb
|
139
|
+
<h1><%= title 'Member Login', 'Here you can login to the site:' %></h1>
|
140
|
+
```
|
141
|
+
|
142
|
+
### Allowed options for `display_meta_tags` and `set_meta_tags` methods
|
143
|
+
|
144
|
+
Use these options to customize the title format:
|
145
|
+
|
146
|
+
* `:site` — site title;
|
147
|
+
* `:title` — page title;
|
148
|
+
* `:description` — page description;
|
149
|
+
* `:keywords` — page keywords;
|
150
|
+
* `:charset` — page character set;
|
151
|
+
* `:prefix` — text between site name and separator;
|
152
|
+
* `:separator` — text used to separate website name from page title;
|
153
|
+
* `:suffix` — text between separator and page title;
|
154
|
+
* `:lowercase` — when true, the page name will be lowercase;
|
155
|
+
* `:reverse` — when true, the page and site names will be reversed;
|
156
|
+
* `:noindex` — add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
157
|
+
* `:nofollow` — add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
158
|
+
* `:follow` – add follow meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
159
|
+
* `:canonical` — add canonical link tag;
|
160
|
+
* `:author` — add author link tag;
|
161
|
+
* `:publisher` — add publisher link tag;
|
162
|
+
* `:prev` — add prev link tag;
|
163
|
+
* `:next` — add next link tag;
|
164
|
+
* `:image_src` — add image_src link tag;
|
165
|
+
* `:og` — add Open Graph tags (Hash);
|
166
|
+
* `:twitter` — add Twitter tags (Hash);
|
167
|
+
* `:refresh` — refresh interval and optionally url to redirect to.
|
168
|
+
|
169
|
+
And here are a few examples to give you ideas.
|
170
|
+
|
171
|
+
```erb
|
172
|
+
<%= display_meta_tags separator: "—".html_safe %>
|
173
|
+
<%= display_meta_tags prefix: false, separator: ":" %>
|
174
|
+
<%= display_meta_tags lowercase: true %>
|
175
|
+
<%= display_meta_tags reverse: true, prefix: false %>
|
176
|
+
<%= display_meta_tags og: { title: 'The Rock', type: 'video.movie' } %>
|
177
|
+
<%= display_meta_tags alternate: { 'zh-Hant' => 'http://example.com.tw/base/url' } %>
|
178
|
+
```
|
179
|
+
|
180
|
+
### Allowed values
|
181
|
+
|
182
|
+
You can specify `:title` as a string or array:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
set_meta_tags title: ['part1', 'part2'], site: 'site'
|
186
|
+
# site | part1 | part2
|
187
|
+
set_meta_tags title: ['part1', 'part2'], reverse: true, site: 'site'
|
188
|
+
# part2 | part1 | site
|
189
|
+
```
|
190
|
+
|
191
|
+
Keywords can be passed as string of comma-separated values, or as an array:
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
set_meta_tags keywords: ['tag1', 'tag2']
|
195
|
+
# tag1, tag2
|
196
|
+
```
|
197
|
+
|
198
|
+
Description is a string (HTML will be stripped from output string).
|
199
|
+
|
200
|
+
### Mirrored values
|
201
|
+
|
202
|
+
Sometimes, it is desirable to mirror meta tag values down into namespaces. A
|
203
|
+
common use case is when you want open graph's `og:title` to be identical to
|
204
|
+
the `title`.
|
205
|
+
|
206
|
+
Say, you have the following in your application layout:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
display_meta_tags og: {
|
210
|
+
title: :title
|
211
|
+
}
|
212
|
+
```
|
213
|
+
|
214
|
+
The value of `og[:title]` is a symbol and therefore references the value of the
|
215
|
+
top level `title` meta tag. With the following in any view:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
title 'my great view'
|
219
|
+
```
|
220
|
+
|
221
|
+
You get this open graph meta tag for free:
|
222
|
+
|
223
|
+
```html
|
224
|
+
<meta property="og:title" content="my great view"></meta>
|
225
|
+
```
|
226
|
+
|
227
|
+
### Using with Turbolinks
|
228
|
+
|
229
|
+
[Turbolinks](https://github.com/turbolinks/turbolinks) is a simple solution for getting
|
230
|
+
the performance benefits of a single-page application without the added complexity of a
|
231
|
+
client-side JavaScript framework. MetaTags supports Turbolinks out of the box, no
|
232
|
+
configuration is necessary.
|
233
|
+
|
234
|
+
### Using with pjax
|
235
|
+
|
236
|
+
[jQuery.pjax](https://github.com/defunkt/jquery-pjax) is a nice solution for navigation
|
237
|
+
without full page reload. The main difference is that layout file will not be rendered,
|
238
|
+
so page title will not change. To fix this, when using a page fragment, pjax will check
|
239
|
+
the fragment DOM element for a `title` or `data-title` attribute and use any value it finds.
|
240
|
+
|
241
|
+
MetaTags simplifies this with `display_title` method, which returns fully resolved
|
242
|
+
page title (include site, prefix/suffix, etc.) But in this case you will have to
|
243
|
+
set default parameters (e.g, `:site`) both in layout file and in your views. To minimize
|
244
|
+
code duplication, you can define a helper in `application_helper.rb`:
|
245
|
+
|
246
|
+
```ruby
|
247
|
+
def default_meta_tags
|
248
|
+
{
|
249
|
+
title: 'Member Login',
|
250
|
+
description: 'Member login page.',
|
251
|
+
keywords: 'Site, Login, Members',
|
252
|
+
separator: "—".html_safe,
|
253
|
+
}
|
254
|
+
end
|
255
|
+
```
|
256
|
+
|
257
|
+
Then in your layout file use:
|
258
|
+
|
259
|
+
```erb
|
260
|
+
<%= display_meta_tags(default_meta_tags) %>
|
261
|
+
```
|
262
|
+
|
263
|
+
And in your pjax templates:
|
264
|
+
|
265
|
+
```erb
|
266
|
+
<!-- set title here, so we can use it both in "display_title" and in "title" -->
|
267
|
+
<% title "My Page title" %>
|
268
|
+
<%= content_tag :div, data: { title: display_title(default_meta_tags) } do %>
|
269
|
+
<h1><%= title %></h1>
|
270
|
+
<!-- HTML goes here -->
|
271
|
+
<% end %>
|
272
|
+
```
|
273
|
+
|
43
274
|
## SEO Basics and MetaTags
|
44
275
|
|
45
276
|
### Titles
|
@@ -499,214 +730,6 @@ set_meta_tags author: [ "Dmytro Shteflyuk", "John Doe" ]
|
|
499
730
|
# <meta name="author" content="John Doe"/>
|
500
731
|
```
|
501
732
|
|
502
|
-
## MetaTags Usage
|
503
|
-
|
504
|
-
First, add this code to your main layout:
|
505
|
-
|
506
|
-
```erb
|
507
|
-
<head>
|
508
|
-
<%= display_meta_tags site: 'My website' %>
|
509
|
-
</head>
|
510
|
-
```
|
511
|
-
|
512
|
-
Then, to set the page title, add this to each of your views (see below for other options):
|
513
|
-
|
514
|
-
```erb
|
515
|
-
<h1><%= title 'My page title' %></h1>
|
516
|
-
```
|
517
|
-
|
518
|
-
When views are rendered, the page title will be included in the right spots:
|
519
|
-
|
520
|
-
```html
|
521
|
-
<head>
|
522
|
-
<title>My website | My page title</title>
|
523
|
-
</head>
|
524
|
-
<body>
|
525
|
-
<h1>My page title</h1>
|
526
|
-
</body>
|
527
|
-
```
|
528
|
-
|
529
|
-
You can find allowed options for `display_meta_tags` method below.
|
530
|
-
|
531
|
-
### Using MetaTags in controller
|
532
|
-
|
533
|
-
You can define following instance variables:
|
534
|
-
|
535
|
-
```ruby
|
536
|
-
@page_title = 'Member Login'
|
537
|
-
@page_description = 'Member login page.'
|
538
|
-
@page_keywords = 'Site, Login, Members'
|
539
|
-
```
|
540
|
-
|
541
|
-
Also you could use `set_meta_tags` method to define all meta tags simultaneously:
|
542
|
-
|
543
|
-
```ruby
|
544
|
-
set_meta_tags title: 'Member Login',
|
545
|
-
description: 'Member login page.',
|
546
|
-
keywords: 'Site, Login, Members'
|
547
|
-
```
|
548
|
-
|
549
|
-
You can find allowed options for `set_meta_tags` method below.
|
550
|
-
|
551
|
-
### Using MetaTags in view
|
552
|
-
|
553
|
-
To set meta tags you can use following methods:
|
554
|
-
|
555
|
-
```erb
|
556
|
-
<% title 'Member Login' %>
|
557
|
-
<% description 'Member login page.' %>
|
558
|
-
<% keywords 'Member login page.' %>
|
559
|
-
<% nofollow %>
|
560
|
-
<% noindex %>
|
561
|
-
<% refresh 3 %>
|
562
|
-
```
|
563
|
-
|
564
|
-
Also there is `set_meta_tags` method exists:
|
565
|
-
|
566
|
-
```erb
|
567
|
-
<% set_meta_tags title: 'Member Login',
|
568
|
-
description: 'Member login page.',
|
569
|
-
keywords: 'Site, Login, Members' %>
|
570
|
-
```
|
571
|
-
|
572
|
-
The `title` method returns title itself, so you can use it to show the title
|
573
|
-
somewhere on the page:
|
574
|
-
|
575
|
-
```erb
|
576
|
-
<h1><%= title 'Member Login' %></h1>
|
577
|
-
```
|
578
|
-
|
579
|
-
If you want to set the title and display another text, use this:
|
580
|
-
|
581
|
-
```erb
|
582
|
-
<h1><%= title 'Member Login', 'Here you can login to the site:' %></h1>
|
583
|
-
```
|
584
|
-
|
585
|
-
### Allowed options for `display_meta_tags` and `set_meta_tags` methods
|
586
|
-
|
587
|
-
Use these options to customize the title format:
|
588
|
-
|
589
|
-
* `:site` — site title;
|
590
|
-
* `:title` — page title;
|
591
|
-
* `:description` — page description;
|
592
|
-
* `:keywords` — page keywords;
|
593
|
-
* `:charset` — page character set;
|
594
|
-
* `:prefix` — text between site name and separator;
|
595
|
-
* `:separator` — text used to separate website name from page title;
|
596
|
-
* `:suffix` — text between separator and page title;
|
597
|
-
* `:lowercase` — when true, the page name will be lowercase;
|
598
|
-
* `:reverse` — when true, the page and site names will be reversed;
|
599
|
-
* `:noindex` — add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
600
|
-
* `:nofollow` — add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
601
|
-
* `:follow` – add follow meta tag; when true, 'robots' will be used, otherwise the string will be used;
|
602
|
-
* `:canonical` — add canonical link tag;
|
603
|
-
* `:author` — add author link tag;
|
604
|
-
* `:publisher` — add publisher link tag;
|
605
|
-
* `:prev` — add prev link tag;
|
606
|
-
* `:next` — add next link tag;
|
607
|
-
* `:image_src` — add image_src link tag;
|
608
|
-
* `:og` — add Open Graph tags (Hash);
|
609
|
-
* `:twitter` — add Twitter tags (Hash);
|
610
|
-
* `:refresh` — refresh interval and optionally url to redirect to.
|
611
|
-
|
612
|
-
And here are a few examples to give you ideas.
|
613
|
-
|
614
|
-
```erb
|
615
|
-
<%= display_meta_tags separator: "—".html_safe %>
|
616
|
-
<%= display_meta_tags prefix: false, separator: ":" %>
|
617
|
-
<%= display_meta_tags lowercase: true %>
|
618
|
-
<%= display_meta_tags reverse: true, prefix: false %>
|
619
|
-
<%= display_meta_tags og: { title: 'The Rock', type: 'video.movie' } %>
|
620
|
-
<%= display_meta_tags alternate: { 'zh-Hant' => 'http://example.com.tw/base/url' } %>
|
621
|
-
```
|
622
|
-
|
623
|
-
### Allowed values
|
624
|
-
|
625
|
-
You can specify `:title` as a string or array:
|
626
|
-
|
627
|
-
```ruby
|
628
|
-
set_meta_tags title: ['part1', 'part2'], site: 'site'
|
629
|
-
# site | part1 | part2
|
630
|
-
set_meta_tags title: ['part1', 'part2'], reverse: true, site: 'site'
|
631
|
-
# part2 | part1 | site
|
632
|
-
```
|
633
|
-
|
634
|
-
Keywords can be passed as string of comma-separated values, or as an array:
|
635
|
-
|
636
|
-
```ruby
|
637
|
-
set_meta_tags keywords: ['tag1', 'tag2']
|
638
|
-
# tag1, tag2
|
639
|
-
```
|
640
|
-
|
641
|
-
Description is a string (HTML will be stripped from output string).
|
642
|
-
|
643
|
-
### Mirrored values
|
644
|
-
|
645
|
-
Sometimes, it is desirable to mirror meta tag values down into namespaces. A
|
646
|
-
common use case is when you want open graph's `og:title` to be identical to
|
647
|
-
the `title`.
|
648
|
-
|
649
|
-
Say, you have the following in your application layout:
|
650
|
-
|
651
|
-
```ruby
|
652
|
-
display_meta_tags og: {
|
653
|
-
title: :title
|
654
|
-
}
|
655
|
-
```
|
656
|
-
|
657
|
-
The value of `og[:title]` is a symbol and therefore references the value of the
|
658
|
-
top level `title` meta tag. With the following in any view:
|
659
|
-
|
660
|
-
```ruby
|
661
|
-
title 'my great view'
|
662
|
-
```
|
663
|
-
|
664
|
-
You get this open graph meta tag for free:
|
665
|
-
|
666
|
-
```html
|
667
|
-
<meta property="og:title" content="my great view"></meta>
|
668
|
-
```
|
669
|
-
|
670
|
-
### Using with pjax
|
671
|
-
|
672
|
-
[jQuery.pjax](https://github.com/defunkt/jquery-pjax) is a nice solution for navigation
|
673
|
-
without full page reload. The main difference is that layout file will not be rendered,
|
674
|
-
so page title will not change. To fix this, when using a page fragment, pjax will check
|
675
|
-
the fragment DOM element for a `title` or `data-title` attribute and use any value it finds.
|
676
|
-
|
677
|
-
MetaTags simplifies this with `display_title` method, which returns fully resolved
|
678
|
-
page title (include site, prefix/suffix, etc.) But in this case you will have to
|
679
|
-
set default parameters (e.g, `:site`) both in layout file and in your views. To minimize
|
680
|
-
code duplication, you can define a helper in `application_helper.rb`:
|
681
|
-
|
682
|
-
```ruby
|
683
|
-
def default_meta_tags
|
684
|
-
{
|
685
|
-
title: 'Member Login',
|
686
|
-
description: 'Member login page.',
|
687
|
-
keywords: 'Site, Login, Members',
|
688
|
-
separator: "—".html_safe,
|
689
|
-
}
|
690
|
-
end
|
691
|
-
```
|
692
|
-
|
693
|
-
Then in your layout file use:
|
694
|
-
|
695
|
-
```erb
|
696
|
-
<%= display_meta_tags(default_meta_tags) %>
|
697
|
-
```
|
698
|
-
|
699
|
-
And in your pjax templates:
|
700
|
-
|
701
|
-
```erb
|
702
|
-
<!-- set title here, so we can use it both in "display_title" and in "title" -->
|
703
|
-
<% title "My Page title" %>
|
704
|
-
<%= content_tag :div, data: { title: display_title(default_meta_tags) } do %>
|
705
|
-
<h1><%= title %></h1>
|
706
|
-
<!-- HTML goes here -->
|
707
|
-
<% end %>
|
708
|
-
```
|
709
|
-
|
710
733
|
## Author
|
711
734
|
|
712
735
|
[Dmytro Shteflyuk](https://github.com/kpumuk), [https://kpumuk.info](http://kpumuk.info/)
|