Skip to content

Instantly share code, notes, and snippets.

@bradonomics
Forked from josepmartins/inline_svg_helpers.rb
Created January 7, 2021 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradonomics/3e9edc70bc982d26c9428d5d154bc5f5 to your computer and use it in GitHub Desktop.
Save bradonomics/3e9edc70bc982d26c9428d5d154bc5f5 to your computer and use it in GitHub Desktop.
SVG inline helpers for Rails
# Inline svg from @tomeara https://gist.github.com/tomeara/6515860
def inline_svg(path)
file = File.open("app/assets/images/#{path}", "rb")
raw file.read
end
# Use SVG internal link
def use_svg(clazz)
content_tag(:svg, content_tag(:use, "", { "xlink:href" => '#icon-' + clazz }), class: "icon icon-#{clazz}")
end
# Render defs just after body tag
# = inline_svg('icons/svgdefs.svg')
# SVG internal link
#= use_svg('bell')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment