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

- (String) description(description)

Set the page description.

Examples:

description 'This is login page'

See Also:



91
92
93
94
# File 'lib/meta_tags/view_helper.rb', line 91

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

- (String) display_meta_tags(defaults = {})

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>


175
176
177
# File 'lib/meta_tags/view_helper.rb', line 175

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

- (Object) display_title(defaults = {})

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 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' %>">


198
199
200
# File 'lib/meta_tags/view_helper.rb', line 198

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

- (String, Array) keywords(keywords)

Set the page keywords.

Examples:

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

See Also:



73
74
75
76
# File 'lib/meta_tags/view_helper.rb', line 73

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

- (Object) meta_tags

Get meta tags for the page.



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

def meta_tags
  @meta_tags ||= MetaTagsCollection.new
end

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

Set the nofollow meta tag

Examples:

nofollow true
nofollow 'googlebot'

See Also:



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

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'

See Also:



107
108
109
110
# File 'lib/meta_tags/view_helper.rb', line 107

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/"

See Also:



139
140
141
142
# File 'lib/meta_tags/view_helper.rb', line 139

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'

See Also:



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

def set_meta_tags(meta_tags = {})
  self.meta_tags.update(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

See Also:



56
57
58
59
# File 'lib/meta_tags/view_helper.rb', line 56

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