Module: MetaTags::ViewHelper

Defined in:
lib/meta_tags/view_helper.rb

Overview

Contains methods to use in views and helpers.

Instance Method Summary collapse

Instance Method Details

#description(description) ⇒ String

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:



85
86
87
88
# File 'lib/meta_tags/view_helper.rb', line 85

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

#display_meta_tags(default = {}) ⇒ String

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.

Returns:

  • (String)

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
# File 'lib/meta_tags/view_helper.rb', line 146

def display_meta_tags(default = {})
  meta_tags = (default || {}).merge(@meta_tags || {})

  # Prefix (leading space)
  prefix = meta_tags[:prefix] === false ? '' : (meta_tags[:prefix] || ' ')

  # Separator
  separator = meta_tags[:separator].blank? ? '|' : meta_tags[:separator]

  # Suffix (trailing space)
  suffix = meta_tags[:suffix] === false ? '' : (meta_tags[:suffix] || ' ')

  # Title
  title = meta_tags[:title]
  if meta_tags[:lowercase] === true and !title.blank?
    title = if title.is_a?(Array)
      title.map { |t| t.downcase }
    else
      title.downcase
    end
  end

  result = []

  # title
  if title.blank?
    result << (:title, meta_tags[:site])
  else
    title = normalize_title(title)
    title = [meta_tags[:site]] + title
    title.reverse! if meta_tags[:reverse] === true
    sep = prefix + separator + suffix
    result << (:title, title.join(sep))
  end

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

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

  # noindex & nofollow
  noindex_name = meta_tags[:noindex].is_a?(String) ? meta_tags[:noindex] : 'robots'
  nofollow_name = meta_tags[:nofollow].is_a?(String) ? meta_tags[:nofollow] : 'robots'

  if noindex_name == nofollow_name
    content = [(meta_tags[:noindex] ? 'noindex' : nil), (meta_tags[:nofollow] ? 'nofollow' : nil)].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]
    result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if meta_tags[:nofollow]
  end

  # canonical
  result << tag(:link, :rel => :canonical, :href => meta_tags[:canonical]) unless meta_tags[:canonical].blank?

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

#keywords(keywords) ⇒ String, Array

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:



67
68
69
70
# File 'lib/meta_tags/view_helper.rb', line 67

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

#nofollow(nofollow) ⇒ Boolean, String

Set the nofollow meta tag

Examples:

nofollow true
nofollow 'googlebot'

Parameters:

  • nofollow (Boolean, String)

    a nofollow value.

Returns:

  • (Boolean, String)

    passed value.

See Also:



117
118
119
120
# File 'lib/meta_tags/view_helper.rb', line 117

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

#noindex(noindex) ⇒ Boolean, String

Set the noindex meta tag

Examples:

noindex true
noindex 'googlebot'

Parameters:

  • noindex (Boolean, String)

    a noindex value.

Returns:

  • (Boolean, String)

    passed value.

See Also:



101
102
103
104
# File 'lib/meta_tags/view_helper.rb', line 101

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

#set_meta_tags(meta_tags = {}) ⇒ Object

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:



23
24
25
26
# File 'lib/meta_tags/view_helper.rb', line 23

def set_meta_tags(meta_tags = {})
  @meta_tags ||= {}
  @meta_tags.merge!(meta_tags || {})
end

#title(title, headline = '') ⇒ String

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"

Parameters:

  • title (String, Array)

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

  • 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:



50
51
52
53
# File 'lib/meta_tags/view_helper.rb', line 50

def title(title, headline = '')
  set_meta_tags(:title => title)
  headline.blank? ? title : headline
end