大したコードではありませんが、Glyphicon用のRailsヘルパーを書きましたので残しておこうと思います。
メソッドはfont-awesomeのGemを参考としています。fa_icon
メソッドが便利だったので、Glyphicon用のヘルパーも作ろうと思ったのがきっかけです。
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_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が有名ですが、どちらも良いところがあるので見た目の好き嫌いと値段を見て判断すればよいと思います!