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:

  • description (String)

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

Returns:

  • (String)

    passed value.

See Also:



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

def description(description)
  set_meta_tags(description: description)
  description
end

#display_meta_tags(defaults = {}) ⇒ String

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

Examples:

Render meta tags in a layout

display_meta_tags site: 'My website'

ERB layout usage

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

Parameters:

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

    default meta tag values.

  • default (Hash)

    a customizable set of options

Returns:

  • (String)

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



164
165
166
# File 'lib/meta_tags/view_helper.rb', line 164

def display_meta_tags(defaults = {})
  meta_tags.with_defaults(defaults) { Renderer.new(meta_tags).render(self) }
end

#display_title(defaults = {}) ⇒ Object

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 layout file (with the display_meta_tags helper) will not be rendered, so you have to pass default arguments such as the site title here. You probably want to define a helper with default options to minimize code duplication.

Examples:

Build a PJAX-compatible title string

display_title title: 'My Page', site: 'PJAX Site'

ERB PJAX container usage

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

Parameters:

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

    list of meta tags.

  • default (Hash)

    a customizable set of options



192
193
194
# File 'lib/meta_tags/view_helper.rb', line 192

def display_title(defaults = {})
  meta_tags.full_title(defaults)
end

#keywords(keywords) ⇒ String, Array

Set the legacy keywords meta tag.

Modern search engines ignore this tag, but some older integrations and internal systems may still read it.

Examples:

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

Parameters:

  • keywords (String, Array)

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

Returns:

  • (String, Array)

    passed value.

See Also:



69
70
71
72
# File 'lib/meta_tags/view_helper.rb', line 69

def keywords(keywords)
  set_meta_tags(keywords: keywords)
  keywords
end

#meta_tagsObject

Get meta tags for the page.



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

def meta_tags
  @meta_tags ||= MetaTagsCollection.new
end

#nofollow(nofollow = true) ⇒ Boolean, ...

Set the nofollow meta tag.

Examples:

nofollow true
nofollow 'googlebot'

Parameters:

  • nofollow (Boolean, String, Array<String>) (defaults to: true)

    a nofollow value.

Returns:

  • (Boolean, String, Array<String>)

    passed value.

See Also:



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

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

#noindex(noindex = true) ⇒ Boolean, ...

Set the noindex meta tag.

Examples:

noindex true
noindex 'googlebot'

Parameters:

  • noindex (Boolean, String, Array<String>) (defaults to: true)

    a noindex value.

Returns:

  • (Boolean, String, Array<String>)

    passed value.

See Also:



97
98
99
100
# File 'lib/meta_tags/view_helper.rb', line 97

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

#refresh(refresh) ⇒ Integer, String

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:



123
124
125
126
# File 'lib/meta_tags/view_helper.rb', line 123

def refresh(refresh)
  set_meta_tags(refresh: refresh)
  refresh
end

#set_meta_tags(meta_tags = {}) ⇒ Object

Set meta tags for the page.

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

Usually you will not call this method directly. Use helpers like #title, #description, #noindex, and #canonical for daily tasks. #keywords remains available for legacy compatibility.

Examples:

set_meta_tags title: 'Login Page', description: 'Here you can login'
set_meta_tags canonical: 'https://example.com/login'

Parameters:

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

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

See Also:



27
28
29
# File 'lib/meta_tags/view_helper.rb', line 27

def set_meta_tags(meta_tags = {})
  self.meta_tags.update(meta_tags)
end

#title(title = nil, headline = "") ⇒ String

Set the page title and return it.

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 “Login Page”, return “Login Page”

title 'Login Page'

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

title 'Login Page', 'Please login'

Set title as array of strings

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 using configured separator value (see #display_meta_tags). When nil, current title will be returned.

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

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

Returns:

  • (String)

    returns title value or headline if passed.

See Also:



52
53
54
55
# File 'lib/meta_tags/view_helper.rb', line 52

def title(title = nil, headline = "")
  set_meta_tags(title: title) unless title.nil?
  headline.presence || meta_tags.page_title
end