百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

metamagic

> 编程语言
开源

用于创建元标记的简单 Ruby on Rails 插件。

437 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

用于创建元标记的简单 Ruby on Rails 插件。

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:

ruby
gem 'metamagic'

Then run bundle install.

Examples

Basic usage

At the top of your view, e.g. app/views/posts/show.html.erb:

erb

And in app/views/layouts/application.html.erb:

erb

  
  ...

This will generate the following:

xml

  My Title
  
  
  ...

Using templates

Templates can be used to insert meta values from your views into your layouts. This is usable for example if you want to add something to your title on all pages, or have some default keywords added to all pages. Templates work with all tag types, including OpenGraph, Twitter, and others.

See below for examples on using templates.

Title templates

Title templates can be used to automatically insert the name of your site into the meta title.

In your template:

erb

In your layout:

erb

This will render the following:

xml

  My Title — My Site
  ...

The default separator is -.

If you hadn't set the title in your view, it would just display the site name.

You can also use a proc to enable custom processing:

erb
 { title.include?(site) ? title : "#{title} — #{site}" }
%>

This will insert the site name only if it is not already present in the title.

You could also do this with a view helper. I use this in one of my websites:

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

The proc is still needed in the layout to ensure the right context for the template:

erb
 { meta_title_for(title, site) }
%>

Keywords template

Keyword templates can be used to add some default keywords to all pages on your site.

In your template:

erb

In your layout:

erb

This will render the following:

xml

  
  ...

Adding templates for other tag types

Templates are supported on all tag types. You can access the values set in the view by replacing colons (:) in your meta keys with underscores (_), so for example og:image can be accessed with og_image.

Shortcut helpers

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

erb

This will generate the following:

xml

  My Title
  
  
  ...

Shortcut helpers return the value you send to them, so you can dry up your code by setting the title once in e.g. a tag, like this:

erb

This is the same as setting the title at the top of your view.

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:

erb

  
  ...

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:

erb

This will generate the following:

xml

  ...
  
  ...

Adding relation links

You can add `` tags like this:

erb

Or using the rel shortcut helper:

erb

This will generate the following:

xml

  ...
  
  
  ...

Custom properties

OpenGraph (Facebook)

erb

This will generate the following:

xml

  ...
  
  ...

The above can also be written with the shortcut helper:

erb

Twitter Cards

erb

This will generate the following:

xml

  ...
  
  
  ...

The above can also be written with the shortcut helper:

erb

Other custom properties

You can add custom properties like this:

erb

This will generate the following:

xml

  ...
  
  
  
  
  ...

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

erb

Custom tags

You can add custom rendering for tag prefixes you specify.

In config/initializers/metamagic.rb:

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

In your view:

erb

This will render the following:

xml
My Title

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

erb

Requirements

  • Rails 3.0 or above
  • Ruby 1.9 or above

Contributing

  1. Fork the project
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes, including tests, 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

  • See the list of contributors

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

Issues· 8 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言