Module: MetaTags::ViewHelper

Defined in:
lib/meta_tags/view_helper.rb

Overview

Contains methods to use in views and helpers.

Constant Summary

[:canonical, :prev, :next]

Instance Method Summary (collapse)

Instance Method Details

- (String) description(description)

Set the page description.

Examples:

description 'This is login page'

Parameters:

  • page (String)

    description to be set in HEAD section of the HTML document. Please note, any HTML tags will be stripped from output string, and string will be truncated to 200 characters.

Returns:

  • (String)

    passed value.

See Also:



94
95
96
97
# File 'lib/meta_tags/view_helper.rb', line 94

def description(description)
  set_meta_tags(:description => description)
  description
end

- (String) display_meta_tags(default = {})

Set default meta tag values and display meta tags. This method should be used in layout file.

Examples:

<head>
  <%= display_meta_tags :site => 'My website' %>
</head>

Parameters:

  • default (Hash) (defaults to: {})

    default meta tag values.

Options Hash (default):

  • :site (String) — default: nil

    site title;

  • :title (String) — default: ""

    page title;

  • :description (String) — default: nil

    page description;

  • :keywords (String) — default: nil

    page keywords;

  • :prefix (String, Boolean) — default: " "

    text between site name and separator; when false, no prefix will be rendered;

  • :separator (String) — default: "|"

    text used to separate website name from page title;

  • :suffix (String, Boolean) — default: " "

    text between separator and page title; when false, no suffix will be rendered;

  • :lowercase (Boolean) — default: false

    when true, the page name will be lowercase;

  • :reverse (Boolean) — default: false

    when true, the page and site names will be reversed;

  • :noindex (Boolean, String) — default: false

    add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used;

  • :nofollow (Boolean, String) — default: false

    add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used;

  • :canonical (String) — default: nil

    add canonical link tag.

  • :prev (String) — default: nil

    add prev link tag;

  • :next (String) — default: nil

    add next link tag.

  • :refresh (String, Integer) — default: nil

    meta refresh tag;

  • :open_graph (Hash) — default: {}

    add Open Graph meta tags.

Returns:

  • (String)

    HTML meta tags to render in HEAD section of the HTML document.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/meta_tags/view_helper.rb', line 175

def display_meta_tags(default = {})
  meta_tags = normalize_open_graph(default).deep_merge!(self.meta_tags)

  result = []

  # title
  title = build_full_title(meta_tags)
  result << (:title, title) unless title.blank?

  # description
  description = normalize_description(meta_tags.delete(:description))
  result << tag(:meta, :name => :description, :content => description) unless description.blank?

  # keywords
  keywords = normalize_keywords(meta_tags.delete(:keywords))
  result << tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank?

  # noindex & nofollow
  noindex_name  = String === meta_tags[:noindex]  ? meta_tags[:noindex]  : 'robots'
  nofollow_name = String === meta_tags[:nofollow] ? meta_tags[:nofollow] : 'robots'

  if noindex_name == nofollow_name
    content = [meta_tags[:noindex] && 'noindex', meta_tags[:nofollow] && 'nofollow'].compact.join(', ')
    result << tag(:meta, :name => noindex_name, :content => content) unless content.blank?
  else
    result << tag(:meta, :name => noindex_name,  :content => 'noindex')  if meta_tags[:noindex] && meta_tags[:noindex] != false
    result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow] && meta_tags[:nofollow] != false
  end
  meta_tags.delete(:noindex)
  meta_tags.delete(:nofollow)

  # refresh
  if refresh = meta_tags.delete(:refresh)
    result << tag(:meta, 'http-equiv' => 'refresh', :content => refresh.to_s) unless refresh.blank?
  end

  # hashes
  meta_tags.each do |property, data|
    if data.is_a?(Hash)
      result.concat process_tree(property, data)
      meta_tags.delete(property)
    end
  end

  # canonical, prev and next
  [ :canonical, :prev, :next ].each do |tag_name|
    next unless href = meta_tags.delete(tag_name)
    result << tag(:link, :rel => tag_name, :href => href)
  end

  # user defined
  meta_tags.each do |name, data|
    Array(data).each do |val|
      result << tag(:meta, :name => name, :content => val)
    end
    meta_tags.delete(name)
  end

  result = result.join("\n")
  result.respond_to?(:html_safe) ? result.html_safe : result
end

- (Object) display_title(default = {})

Returns full page title as a string without surrounding <title> tag.

The only case when you may need this helper is when you use pjax. This means that your layour file (with display_meta_tags helper) will not be rendered, so you have to pass default arguments like site title in here. You probably want to define helper with default options to minimize code duplication.

Examples:

<div data-page-container="true" title="<%= display_title :title => 'My Page', :site => 'PJAX Site' %>">

Parameters:

  • meta_tags (Hash)

    list of meta tags.

  • default (Hash) (defaults to: {})

    a customizable set of options

Options Hash (default):

  • :site (String) — default: nil

    site title;

  • :title (String) — default: ""

    page title;

  • :prefix (String, Boolean) — default: " "

    text between site name and separator; when false, no prefix will be rendered;

  • :separator (String) — default: "|"

    text used to separate website name from page title;

  • :suffix (String, Boolean) — default: " "

    text between separator and page title; when false, no suffix will be rendered;

  • :lowercase (Boolean) — default: false

    when true, the page name will be lowercase;

  • :reverse (Boolean) — default: false

    when true, the page and site names will be reversed;



256
257
258
259
# File 'lib/meta_tags/view_helper.rb', line 256

def display_title(default = {})
  meta_tags = normalize_open_graph(default).deep_merge!(self.meta_tags)
  build_full_title(meta_tags)
end

- (String, Array) keywords(keywords)

Set the page keywords.

Examples:

keywords 'keyword1, keyword2'
keywords %w(keyword1 keyword2)

Parameters:

  • keywords (String, Array)

    meta keywords to render in HEAD section of the HTML document.

Returns:

  • (String, Array)

    passed value.

See Also:



76
77
78
79
# File 'lib/meta_tags/view_helper.rb', line 76

def keywords(keywords)
  set_meta_tags(:keywords => keywords)
  keywords
end

- (Object) meta_tags

Get meta tags for the page.



8
9
10
# File 'lib/meta_tags/view_helper.rb', line 8

def meta_tags
  @meta_tags ||= HashWithIndifferentAccess.new
end

- (Boolean, String) nofollow(nofollow = true)

Set the nofollow meta tag

Examples:

nofollow true
nofollow 'googlebot'

Parameters:

  • nofollow (Boolean, String) (defaults to: true)

    a nofollow value.

Returns:

  • (Boolean, String)

    passed value.

See Also:



126
127
128
129
# File 'lib/meta_tags/view_helper.rb', line 126

def nofollow(nofollow = true)
  set_meta_tags(:nofollow => nofollow)
  nofollow
end

- (Boolean, String) noindex(noindex = true)

Set the noindex meta tag

Examples:

noindex true
noindex 'googlebot'

Parameters:

  • noindex (Boolean, String) (defaults to: true)

    a noindex value.

Returns:

  • (Boolean, String)

    passed value.

See Also:



110
111
112
113
# File 'lib/meta_tags/view_helper.rb', line 110

def noindex(noindex = true)
  set_meta_tags(:noindex => noindex)
  noindex
end

- (Integer, String) refresh(refresh)

Set the refresh meta tag

Examples:

refresh 5
refresh "5;url=http://www.example.com/"

Parameters:

  • refresh (Integer, String)

    a refresh value.

Returns:

  • (Integer, String)

    passed value.

See Also:



142
143
144
145
# File 'lib/meta_tags/view_helper.rb', line 142

def refresh(refresh)
  set_meta_tags(:refresh => refresh)
  refresh
end

- (Object) set_meta_tags(meta_tags = {})

Set meta tags for the page.

Method could be used several times, and all options passed will be merged. If you will set the same property several times, last one will take precedence.

Usually you will not call this method directly. Use #title, #keywords, #description for your daily tasks.

Examples:

set_meta_tags :title => 'Login Page', :description => 'Here you can login'
set_meta_tags :keywords => 'authorization, login'

Parameters:

  • meta_tags (Hash) (defaults to: {})

    list of meta tags. See #display_meta_tags for allowed options.

See Also:



30
31
32
# File 'lib/meta_tags/view_helper.rb', line 30

def set_meta_tags(meta_tags = {})
  self.meta_tags.deep_merge! normalize_open_graph(meta_tags)
end

- (String) title(title = nil, headline = '')

Set the page title and return it back.

This method is best suited for use in helpers. It sets the page title and returns it (or headline if specified).

Examples:

Set HTML title to “Please login”, return “Please login”

title 'Login Page'

Set HTML title to “Login Page”, return “Please login”

title 'Login Page', 'Please login'

Set title as array of strings

title :title => ['part1', 'part2'] # => "part1 | part2"

Get current title

title

Parameters:

  • title (nil, String, Array) (defaults to: nil)

    page title. When passed as an Array, parts will be joined divided with configured separator value (see #display_meta_tags). When nil, current title will be returned.

  • headline (String) (defaults to: '')

    the value to return from method. Useful for using this method in views to set both page title and the content of heading tag.

Returns:

  • (String)

    returns title value or headline if passed.

See Also:



59
60
61
62
# File 'lib/meta_tags/view_helper.rb', line 59

def title(title = nil, headline = '')
  set_meta_tags(:title => title) unless title.nil?
  headline.blank? ? meta_tags[:title] : headline
end