LoginSignup
0
0

More than 5 years have passed since last update.

Glyphicons、Halflings用のRailsヘルパー

Posted at

大したコードではありませんが、Glyphicon用のRailsヘルパーを書きましたので残しておこうと思います。

メソッドはfont-awesomeのGemを参考としています。fa_iconメソッドが便利だったので、Glyphicon用のヘルパーも作ろうと思ったのがきっかけです。

bokmann/font-awesome-rails

GlyphiconsHelper

app/helpers/glyphicons_helper.rb
module GlyphiconsHelper
  def glyphicons_icon(name = "", options = {})
    classes = ["glyphicons", "glyphicons-#{name}"]
    classes.concat Array(options[:class])
    icon_join(content_tag(:span, nil, class: classes), options[:text], options[:right])
  end

  def halflings_icon(name = "", options = {})
    classes = ["halflings", "halflings-#{name}"]
    classes.concat Array(options[:class])
    icon_join(content_tag(:span, nil, class: classes), options[:text], options[:right])
  end

  private
  def icon_join(icon, text, right_icon)
    elements = [icon, ERB::Util.html_escape(text)]
    elements.reverse! if right_icon
    safe_join(elements, " ")
  end
end

※ご利用は自己責任でお願いします!

使い方

アセットの読み込み

事前にフォントとCSSファイルを読みこんでおく必要があります。ここでは割愛しますので、公式ドキュメントをご参照ください。

Glyphicons

ヘルパーメソッド

以下のような書式で使えます。

= glyphicons_icon("pencil")
# => <span class="glyphicons glyphicons-pencil"></span>

= halflings_icon("pencil", text: "テキスト", right: true, class: ["class1", "class2"])
# => テキスト <span class="halflings halflings-pencil class1 class2"></span>

とりあえず標準のGlyphiconsとGlyphiconHalflingsにのみ対応させています。

基本的にはどちらも有償のフォントとなりますが、Bootstrap3にはフリーのHalflingsが組み込まれていますね。

フリーのフォントアイコンはFontAwesomeが有名ですが、どちらも良いところがあるので見た目の好き嫌いと値段を見て判断すればよいと思います!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0