Module: SiteMetaHelper
- Defined in:
- lib/app/helpers/site_meta_helper.rb
Overview
Provides helpers for easily adding metadata to your views and layouts.
In your layout use helpers head_title
, meta_description
, meta_keywords
with optional defaults. In each view you can override the defaults using helpers set_head_title
, set_meta_description
, set_meta_keywords
.
Providing description meta
In your layout use meta_description
in head section of HTML with optional default description:
%head
= meta_description("Site_meta is the easiest way to provide metadata in your Rails application")
In each view you can override default setting using set_meta_description
. In view app/views/pages/show.html.haml
- (h(@page.description))
Providing keywords meta
In your layout use meta_keywords
in head section of HTML with optional list of default keywords:
%head
= meta_keywords("rails,gem,plugin,ruby,meta,metadata,html")
In each view you can override or add keywords using set_meta_keywords
. In view app/views/pages/show.html.haml
- (h(@page..join(",")))
which will add keywords to the default set or
- ("pricing,information", :replace)
to replace the whole list.
Providing title
In your layout use head_title
in head section and provide last element of breadcrumb trail (usually site name)
%head
= head_title("Site_meta")
In each view you provide title using set_head_title
, where you can provide more elements of breadcrumbs. In view app/views/pages/show.html.haml
- set_head_title(h(@page.title))
or in app/views/pages/edit.html.haml
- set_head_title("Editing #{h(@page.title)}", "Pages")
This will result in title set to:
"Page title - Site_meta"
in first case and:
"Editing Page title - Pages - Site_meta"
in second case
Providing title in HTML
Title that is presented in the body part of HTML doesn’t need the breadcrumbs and may differ from the title presented in HTML head part. Use page_title
helper in your layout:
%body
%h1= page_title
In each view set the page title using set_page_title
helper. In view app/views/pages/show.html.haml:
- set_page_title(h(@page.title))
Providing head and page title at the same time
In most situations (like in case of show.html.haml mentioned above) you want to have the same title used as a page title and as a beginning of the breadcrumb trail. In that case use set_head_and_page_title
helper. In view app/views/pages/show.html.haml
- set_head_and_page_title(h(@page.title), "Pages")
This will set page title to first element and set proper trail in head.
Providing encoding
To provide encoding information use meta_content_type
in your layout use
%head
= meta_content_type
This will provide utf-8 charset by default.
Instance Method Summary collapse
-
#head_title(default_title, separator = " - ") ⇒ Object
Returns string with title tag to use in html head.
-
#meta_content_type(type = :utf) ⇒ Object
Returns meta tag with appropriate encoding specified.
-
#meta_description(default_description = nil) ⇒ Object
Returns description meta tag with
default_description
to use in html head. -
#meta_keywords(default_keywords = []) ⇒ Object
Returns keywords meta tag with
default_keywords
to use in html head. -
#meta_tag(name, content, key = 'name') ⇒ Object
:nodoc:.
-
#page_title ⇒ Object
Returns page title set using
set_page_title
. -
#set_head_and_page_title(*title) ⇒ Object
Sets head title breadcrumbs trail and page title to the leftmost element.
-
#set_head_title(*title) ⇒ Object
Sets head title breadcrumbs trail.
-
#set_meta_description(description) ⇒ Object
Sets meta
description
to use withmeta_description
. -
#set_meta_keywords(keywords, merge_mode = :merge) ⇒ Object
Sets
keywords
to use withmeta_keywords
. -
#set_page_title(title) ⇒ Object
Sets page title to use with
page_title
. -
#split_keywords(keywords) ⇒ Object
:nodoc:.
-
#title_tag(content) ⇒ Object
:nodoc:.
Instance Method Details
#head_title(default_title, separator = " - ") ⇒ Object
Returns string with title tag to use in html head. It uses default_title
unless you provide breadcrumb elements using set_head_title
or set_head_and_page_title
. Elements of title (with default_title
being the last one) are joined using separator
.
112 113 114 115 |
# File 'lib/app/helpers/site_meta_helper.rb', line 112 def head_title(default_title, separator=" - ") title = [@head_title, default_title].flatten.compact.join(separator) title_tag(title) end |
#meta_content_type(type = :utf) ⇒ Object
Returns meta tag with appropriate encoding specified. type
may be any string, it’s inserted verbatim. Popular encodings include “utf-8”, “iso-8859-1” and others.
103 104 105 106 |
# File 'lib/app/helpers/site_meta_helper.rb', line 103 def (type=:utf) type = "utf-8" if type == :utf ("Content-Type", "text/html; charset=#{type}", "http-equiv") end |
#meta_description(default_description = nil) ⇒ Object
Returns description meta tag with default_description
to use in html head. You can override it on per-view base using set_meta_description
. Returns nil when default_description
is nil and you did not provide one using set_meta_description
.
121 122 123 124 125 126 |
# File 'lib/app/helpers/site_meta_helper.rb', line 121 def (default_description=nil) description = @meta_description || default_description if description (:description, description) end end |
#meta_keywords(default_keywords = []) ⇒ Object
Returns keywords meta tag with default_keywords
to use in html head. You can override keywords on per-view base using set_meta_keywords
. Optional default_keywords
may be a comma separated string or array of strings. Returns nil when default_keywords
is nil and none additional keywords were provided through set_meta_keywords
.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/app/helpers/site_meta_helper.rb', line 133 def (default_keywords=[]) merge_mode = @meta_keywords_merge_mode || :merge = split_keywords(@meta_keywords) keywords = if merge_mode == :merge default_keywords = split_keywords(default_keywords) ( + default_keywords).uniq else end unless keywords.empty? (:keywords, keywords.join(",")) end end |
#meta_tag(name, content, key = 'name') ⇒ Object
:nodoc:
190 191 192 |
# File 'lib/app/helpers/site_meta_helper.rb', line 190 def (name, content, key='name') #:nodoc: tag 'meta', key => name, :content => content end |
#page_title ⇒ Object
Returns page title set using set_page_title
149 150 151 |
# File 'lib/app/helpers/site_meta_helper.rb', line 149 def page_title @page_title end |
#set_head_and_page_title(*title) ⇒ Object
Sets head title breadcrumbs trail and page title to the leftmost element.
159 160 161 162 163 |
# File 'lib/app/helpers/site_meta_helper.rb', line 159 def set_head_and_page_title(*title) title = [title].flatten.compact set_head_title title set_page_title title.first end |
#set_head_title(*title) ⇒ Object
Sets head title breadcrumbs trail
154 155 156 |
# File 'lib/app/helpers/site_meta_helper.rb', line 154 def set_head_title(*title) @head_title = title.flatten end |
#set_meta_description(description) ⇒ Object
Sets meta description
to use with meta_description
.
171 172 173 |
# File 'lib/app/helpers/site_meta_helper.rb', line 171 def (description) @meta_description = description end |
#set_meta_keywords(keywords, merge_mode = :merge) ⇒ Object
Sets keywords
to use with meta_keywords
. Optional merge_mode
may be set to either :merge (default) or :replace. In the second mode provided keywords are not merged with defaults when output using meta_keywords
.
178 179 180 181 182 |
# File 'lib/app/helpers/site_meta_helper.rb', line 178 def (keywords, merge_mode=:merge) raise(ArgumentError, "Allowed merge modes are only :replace, :merge") unless [:replace,:merge].include?(merge_mode) @meta_keywords = keywords @meta_keywords_merge_mode = merge_mode end |
#set_page_title(title) ⇒ Object
Sets page title to use with page_title
166 167 168 |
# File 'lib/app/helpers/site_meta_helper.rb', line 166 def set_page_title(title) @page_title = title end |
#split_keywords(keywords) ⇒ Object
:nodoc:
184 185 186 187 188 |
# File 'lib/app/helpers/site_meta_helper.rb', line 184 def split_keywords(keywords) #:nodoc: return [] unless keywords keywords = keywords.split(",") if keywords.is_a? String keywords.flatten.map {|k| k.strip} end |
#title_tag(content) ⇒ Object
:nodoc:
194 195 196 |
# File 'lib/app/helpers/site_meta_helper.rb', line 194 def title_tag(content) #:nodoc: content_tag "title", content end |