Method: MetaTags::ViewHelper#display_meta_tags

Defined in:
lib/meta_tags/view_helper.rb

#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