Build Status

Meta Magic Motherfuckers

Metamagic

Metamagic is a simple Ruby on Rails plugin for creating meta tags. It supports regular meta tags, OpenGraph (Facebook), Twitter Cards, and custom tags.

See the changelog for changes in version 3.0.

Installation

In your Gemfile:

gem 'metamagic', '3.0.0.beta2'

Then run bundle install.

Examples

Basic usage

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>

Shortcut helpers

For easy setting of meta tags, you can use the shortcut helpers like this:

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

Note: Shortcut helpers will never override methods already present in the view context, so for example if you have a method named title, this will not be overridden.

Specifying default meta tag values

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.

Custom meta tags

For custom meta tags, you can use it like this:

<%
meta my_custom_name: "My custom value"
%>

This will generate the following:

<head>
  ...
  <meta content="My custom value" name="my_custom_name" />
  ...
</head>

Custom properties

OpenGraph (Facebook)

<%
meta og: {
  image: "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>

The above can also be written with the shortcut helper:

<%
og image: "http://mydomain.com/images/my_image.jpg"
%>

Twitter Cards

<%
meta twitter: {
  card: "summary",
  site: "@flickr"
}
%>

This will generate the following:

<head>
  ...
  <meta content="summary" property="twitter:card" />
  <meta content="@flickr" property="twitter:site" />
  ...
</head>

The above can also be written with the shortcut helper:

<%
twitter card: "summary",
        site: "@flickr"
%>

Other custom properties

You can add custom properties like this:

<%
meta property: {
  one: "Property One",
  two: "Property Two",
  nested: {
    a: "Nested A",
    b: "Nested B"
  }
}
%>

This will generate the following:

<head>
  ...
  <meta content="Property One" property="one" />
  <meta content="Property Two" property="two" />
  <meta content="Nested A" property="nested:a" />
  <meta content="Nested B" property="nested:b" />
  ...
</head>

The above could also be written with the property shortcut helper:

<%
property one: "Property One",
         two: "Property Two",
         nested: {
           a: "Nested A",
           b: "Nested B"
         }
%>

Custom tags

You can add custom rendering for tag prefixes you specify.

In config/initializers/metamagic.rb:

Metamagic::Renderer.register_tag_type :custom, ->(key, value) { tag(:custom_tag, first: key, second: value) }

In your view:

<%
meta title: "My Title",
     custom: {
       key_one: "My first key",
       key_two: "My second key"
     }
%>

This will render the following:

<title>My Title</title>
<custom_tag first="custom:key_one" second="My first key" />
<custom_tag first="custom:key_two" second="My second key" />

When you register a new tag type, a shortcut helper is automatically defined. The above could therefore also be written as:

<%
custom key_one: "My first key",
       key_two: "My second key"
%>

Requirements

  • Rails 3.0 or above
  • Ruby 1.9 or above

Versioning

Follows semantic versioning.

Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes and make sure the tests pass (run rake)
  4. Commit your changes (git commit -am 'Add new feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new pull request

Contributors

Copyright (c) 2010-2014 Lasse Bunk, released under the MIT license