Module: MetaTags::ViewHelper
- Defined in:
- lib/meta_tags/view_helper.rb
Overview
Contains methods to use in views and helpers.
Constant Summary
- LINK_TAGS =
[:canonical, :prev, :next]
Instance Method Summary (collapse)
-
- (String) description(description)
Set the page description.
-
- (String) display_meta_tags(default = {})
Set default meta tag values and display meta tags.
-
- (Object) display_title(default = {})
Returns full page title as a string without surrounding tag.
-
- (String, Array) keywords(keywords)
Set the page keywords.
-
- (Object) meta_tags
Get meta tags for the page.
-
- (Boolean, String) nofollow(nofollow = true)
Set the nofollow meta tag.
-
- (Boolean, String) noindex(noindex = true)
Set the noindex meta tag.
-
- (Integer, String) refresh(refresh)
Set the refresh meta tag.
-
- (Object) set_meta_tags(meta_tags = {})
Set meta tags for the page.
-
- (String) title(title = nil, headline = '')
Set the page title and return it back.
Instance Method Details
- (String) description(description)
Set the page description.
94 95 96 97 |
# File 'lib/meta_tags/view_helper.rb', line 94 def description(description) (: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.
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 (default = {}) = normalize_open_graph(default).deep_merge!(self.) result = [] # title title = build_full_title() result << content_tag(:title, title) unless title.blank? # description description = normalize_description(.delete(:description)) result << tag(:meta, :name => :description, :content => description) unless description.blank? # keywords keywords = normalize_keywords(.delete(:keywords)) result << tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank? # noindex & nofollow noindex_name = String === [:noindex] ? [:noindex] : 'robots' nofollow_name = String === [:nofollow] ? [:nofollow] : 'robots' if noindex_name == nofollow_name content = [[:noindex] && 'noindex', [: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 [:noindex] && [:noindex] != false result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if [:nofollow] && [:nofollow] != false end .delete(:noindex) .delete(:nofollow) # refresh if refresh = .delete(:refresh) result << tag(:meta, 'http-equiv' => 'refresh', :content => refresh.to_s) unless refresh.blank? end # hashes .each do |property, data| if data.is_a?(Hash) result.concat process_tree(property, data) .delete(property) end end # canonical, prev and next [ :canonical, :prev, :next ].each do |tag_name| next unless href = .delete(tag_name) result << tag(:link, :rel => tag_name, :href => href) end # user defined .each do |name, data| Array(data).each do |val| result << tag(:meta, :name => name, :content => val) end .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.
256 257 258 259 |
# File 'lib/meta_tags/view_helper.rb', line 256 def display_title(default = {}) = normalize_open_graph(default).deep_merge!(self.) build_full_title() end |
- (String, Array) keywords(keywords)
Set the page keywords.
76 77 78 79 |
# File 'lib/meta_tags/view_helper.rb', line 76 def keywords(keywords) (: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 ||= HashWithIndifferentAccess.new end |
- (Boolean, String) nofollow(nofollow = true)
Set the nofollow meta tag
126 127 128 129 |
# File 'lib/meta_tags/view_helper.rb', line 126 def nofollow(nofollow = true) (:nofollow => nofollow) nofollow end |
- (Boolean, String) noindex(noindex = true)
Set the noindex meta tag
110 111 112 113 |
# File 'lib/meta_tags/view_helper.rb', line 110 def noindex(noindex = true) (:noindex => noindex) noindex end |
- (Integer, String) refresh(refresh)
Set the refresh meta tag
142 143 144 145 |
# File 'lib/meta_tags/view_helper.rb', line 142 def refresh(refresh) (: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.
30 31 32 |
# File 'lib/meta_tags/view_helper.rb', line 30 def ( = {}) self..deep_merge! normalize_open_graph() 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).
59 60 61 62 |
# File 'lib/meta_tags/view_helper.rb', line 59 def title(title = nil, headline = '') (:title => title) unless title.nil? headline.blank? ? [:title] : headline end |