Metamagic is a simple Ruby on Rails plugin for creating meta tags.
In your Gemfile:
gem 'metamagic'
Then run bundle install
.
In your app/views/layouts/application.html.erb:
<head>
<%= metamagic %>
...
</head>
Then, at the top of your view, e.g. app/views/posts/show.html.erb:
<%
meta :title => "My title",
:description => "My description",
:keywords => %w(keyword1 keyword2 keyword3)
%>
This will generate the following:
<head>
<title>My title</title>
<meta content="My description" name="description" />
<meta content="keyword1, keyword2, keyword3" name="keywords" />
...
</head>
It's possible to specify default values to be shown if a view doesn't specify its own values. In your app/views/layouts/application.html.erb:
<head>
<%= metamagic :title => "My default title", :description => "My default description.", :keywords => %w(keyword1 keyword2 keyword3) %>
...
</head>
These values are then inserted if a view doesn't set others.
It is possible to add the [defaults]
keyword in a meta
helper that will include the default value set in metamagic
helper if exists.
<%
meta :title => "My page title - [defaults]",
:description => "My page description - [defaults]",
:keywords => %w(my page keywords [defaults])
%>
and
<head>
<%= metamagic :title => "My app title", :description => "My app description", :keywords => %w(my app keywords) %>
...
</head>
This will generate the following:
<head>
<title>My page title - My app title</title>
<meta content="My page description - My app description" name="description" />
<meta content="my, page, keywords, my, app, keywords" name="keywords" />
...
</head>
For custom meta tags, just call it like this in the top of your view:
<%
meta :my_custom_tag => "My custom value"
%>
This will generate the following:
<head>
...
<meta content="My custom value" name="my_custom_tag" />
...
</head>
With custom properties:
<%
meta [:property => "og:image", :content => "http://mydomain.com/images/my_image.jpg"]
%>
This will generate the following:
<head>
...
<meta content="http://mydomain.com/images/my_image.jpg" property="og:image" />
...
</head>
- Rails 3.0 or above
- Ruby 1.9 or above
Follows semantic versioning.
- Fork the project
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new pull r equest
Copyright (c) 2010-2013 Lasse Bunk, released under the MIT license