Metamagic 101 – Introduction, Installation & Usage

Metamagic gem is a RoR plugin for creating and managing meta tags. It auto-generates the meta tags dynamically throughout and cuts down the need for writing it every time.

In addition to the regular meta tags, metamagic also supports custom tags, OpenGraph (Facebook) and Twitter Cards. It is a critical factor to consider while developing web applications.

Let’s Begin With Understanding Meta Tags

Meta tags are pieces of data that provide details – also known as meta-information or metadata – about a particular HTML document. This includes page description, the keywords, details of last modifications and so on. Meta tags are not displayed on the web page. They appear in the page code and are usually found at the ‘head’ section of the web page. The crawler programs that gather information for search engines use meta tags to find and categorise content on web search results pages. Meta tags are not used by all search engines but it is recommended to optimise website meta tags.

The Types Of Meta Tags

They are four major meta tags that are relevant to internet marketing. They are as follows:
Title tags: The title that is displayed for a web page in search engine listings – also displayed on the browser tab of the same page.
Description tags: The description that is displayed for a web page in search engine listings, right below the title tag. A great description with all the important keywords provide the crawler programs information about the web page and can also be used to compel the audience to visit the page.
Keyword tags: The keyword tag includes a list of keywords that are related to the web page.
Alternate text tags: Alternate or Alt tags are brief, textual descriptions of images. They are useful in enhancing SEO on text-only browsers or when the graphics are turned off. Also useful for enhancing the browsing experience of people with various disabilities.

Meta Tags and Search Engine Optimisation

The meta tags fall short of any influence on the ranking of the web page on search engine listings. However, when used properly, it helps the search engine crawler programs to classify, index and display information about the web page, thus helping your web page get listed in the search engine results.

Requirements For Metamagic Gem

  • Rails 3.0 or above
  • Ruby 1.9 or above

Metamagic Gem Installation

  1. Go to the Gemfile and add the gem metamagic
  2. Run bundle install

Basic Usage Example

Let us consider the blogging system. Enter the following at the top of the view, for example, in app/views/blogs/show.html.erb:

<% meta title: "My Blog", description: "Welcome to blogs page", keywords: %w(blogs devcamp portfolio) %>

Enter the following in app/views/layouts/blog.html.erb:


  <%= metamagic %>
  ... 

The above entries will generate the following HTML content:
Ruby on Rails

Templates

Templates are useful for entering meta values from views into layouts. For example, you can use it to add a value or default keyword on all pages. Templates work with all types of tags, including Twitter, OpenGraph and so on.

Title Templates

Title templates are used to automatically insert website name into the meta title.

For example, enter the following in your template:

<% meta title: "My Blog" %>

Enter the following in your layout:

<% metamagic site: "My Site", title: [:blog, :site], separator: " — " %>

The above entries will render the following result:


  
  ...

It can also be used in application helper:

module ApplicationHelper
def meta_title_for(blog, site)
return site if blog.blank?
blog.include?(site) ? blog: "#{blog} — #{site}"
end
End

Keywords Template

Keyword templates are useful for adding default keywords to all the pages on a website.

Want to explore more about RoR development? RailsCarma can help you! Get it touch today.