phoenix_inline_svg v1.4.0 PhoenixInlineSvg.Helpers

This module adds view helpers for rendering SVG files into safe HTML.

To add the helpers, add the following to the quoted view in your my_app_web.ex file.

def view do
  quote do
    use PhoenixInlineSvg.Helpers
  end
end

This will generate functions for each of your images, effectively caching them at compile time.

You can call these functions like so

# Get an image with the default collection
svg_image("image_name")

# Get an image and insert HTML attributes to svg tag
svg_image("image_name", class: "elixir-is-awesome", id: "inline-svg")

# Get an image from a different collection
svg_image("image_name", "collection_name")

# Get an image from a different collection and insert HTML attributes to the svg tag
svg_image("image_name", "collection_name", class: "elixir-is-awesome", id: "inline-svg")

Old Way

As an alternative this module can be imported in the quoted view def of the my_app_web.ex which will always pull the SVG files from the disk (unless you are using a caching module).

def view do
  quote do
    import PhoenixInlineSvg.Helpers
  end
end

Note: If you are setting a custom directory for the SVG files and are using Exrm or Distillery, you will need to ensure that the directory you set is in the outputted lib directory of your application.

Configuration

By default SVG files are loaded from: assets/static/svg/

The directory where SVG files are loaded from can be configured by setting the configuration variable:

config :phoenix_inline_svg, dir: "some/other/dir"

Where some/other/dir is a directory located in the Phoenix application directory.

Link to this section Summary

Functions

The using macro precompiles the SVG images into functions.

Returns a safe HTML string with the contents of the SVG file using the default_collection configuration.

Returns a safe HTML string with the contents of the SVG file after inserting the given HTML attributes.

Returns a safe HTML string with the contents of the SVG file for the given collection after inserting the given HTML attributes.

Link to this section Functions

Link to this macro

__using__(_) (macro)

The using macro precompiles the SVG images into functions.

Examples

# Default collection

svg_image("image_name")
svg_image("image_name", attrs)

# Named collection

svg_image("image_name", "collection_name")
svg_image("image_name", "collection_name", attrs)
Link to this function

svg_image(conn_or_endpoint, name)

Returns a safe HTML string with the contents of the SVG file using the default_collection configuration.

Examples

<%= svg_image(@conn, "home") %>
<%= svg_image(YourAppWeb.Endpoint, "home") %>

Will result in the output:

<svg>...</svg>

The main function is svg_image/4.

Link to this function

svg_image(conn_or_endpoint, name, attrs)

Returns a safe HTML string with the contents of the SVG file after inserting the given HTML attributes.

Examples

<%= svg_image(@conn, "home", class: "logo", id: "bounce-animation") %>
<%= svg_image(YourAppWeb.Endpoint, "home", class: "logo", id: "bounce-animation") %>

Will result in the output:

<svg class="logo" id="bounce-animation">...</svg>

The main function is svg_image/4.

Link to this function

svg_image(conn_or_endpoint, name, collection, attrs \\ [])

Returns a safe HTML string with the contents of the SVG file for the given collection after inserting the given HTML attributes.

Examples

<%= svg_image(@conn, "user", "fontawesome") %>
<%= svg_image(YourAppWeb.Endpoint, "user", "fontawesome") %>

Will result in the output:

<svg>...</svg>

Find SVG file inside of "icons" folder and add class "fa fa-share" and id "bounce-animation"

<%= svg_image(@conn, "user", "icons", class: "fa fa-share", id: "bounce-animation") %>
<%= svg_image(YourAppWeb.Endpoint, "user", "icons", class: "fa fa-share", id: "bounce-animation") %>

Will result in the output:

<svg class="fa fa-share" id="bounce-animation">...</svg>