Module: MetaTags::ViewHelper
- Defined in:
- lib/meta_tags/view_helper.rb
Overview
Contains methods to use in views and helpers.
Instance Method Summary collapse
-
#description(description) ⇒ String
Set the page description.
-
#display_meta_tags(default = {}) ⇒ String
Set default meta tag values and display meta tags.
-
#keywords(keywords) ⇒ String, Array
Set the page keywords.
-
#nofollow(nofollow) ⇒ Boolean, String
Set the nofollow meta tag.
-
#noindex(noindex) ⇒ Boolean, String
Set the noindex meta tag.
-
#set_meta_tags(meta_tags = {}) ⇒ Object
Set meta tags for the page.
-
#title(title, headline = '') ⇒ String
Set the page title and return it back.
Instance Method Details
#description(description) ⇒ String
Set the page description.
85 86 87 88 |
# File 'lib/meta_tags/view_helper.rb', line 85 def description(description) (: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.
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 (default = {}) = (default || {}).merge(@meta_tags || {}) # Prefix (leading space) prefix = [:prefix] === false ? '' : ([:prefix] || ' ') # Separator separator = [:separator].blank? ? '|' : [:separator] # Suffix (trailing space) suffix = [:suffix] === false ? '' : ([:suffix] || ' ') # Title title = [:title] if [: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 << content_tag(:title, [:site]) else title = normalize_title(title) title = [[:site]] + title title.reverse! if [:reverse] === true sep = prefix + separator + suffix result << content_tag(:title, title.join(sep)) end # description description = normalize_description([:description]) result << tag(:meta, :name => :description, :content => description) unless description.blank? # keywords keywords = normalize_keywords([:keywords]) result << tag(:meta, :name => :keywords, :content => keywords) unless keywords.blank? # noindex & nofollow noindex_name = [:noindex].is_a?(String) ? [:noindex] : 'robots' nofollow_name = [:nofollow].is_a?(String) ? [:nofollow] : 'robots' if noindex_name == nofollow_name content = [([:noindex] ? 'noindex' : nil), ([: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 [:noindex] result << tag(:meta, :name => nofollow_name, :content => 'nofollow') if [:nofollow] end # canonical result << tag(:link, :rel => :canonical, :href => [:canonical]) unless [:canonical].blank? result = result.join("\n") result.respond_to?(:html_safe) ? result.html_safe : result end |
#keywords(keywords) ⇒ String, Array
Set the page keywords.
67 68 69 70 |
# File 'lib/meta_tags/view_helper.rb', line 67 def keywords(keywords) (:keywords => keywords) keywords end |
#nofollow(nofollow) ⇒ Boolean, String
Set the nofollow meta tag
117 118 119 120 |
# File 'lib/meta_tags/view_helper.rb', line 117 def nofollow(nofollow) (:nofollow => nofollow) nofollow end |
#noindex(noindex) ⇒ Boolean, String
Set the noindex meta tag
101 102 103 104 |
# File 'lib/meta_tags/view_helper.rb', line 101 def noindex(noindex) (: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.
23 24 25 26 |
# File 'lib/meta_tags/view_helper.rb', line 23 def ( = {}) @meta_tags ||= {} @meta_tags.merge!( || {}) 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).
50 51 52 53 |
# File 'lib/meta_tags/view_helper.rb', line 50 def title(title, headline = '') (:title => title) headline.blank? ? title : headline end |