PhoenixViewAssets
Helps to manage view specific assets in phoenix project. Uses automatic code splitting to avoid over-fetching or downloading assets twice, if they are used in multiple views. Also supports phoenix live reload.
Installation
Add phoenix_view_assets to your list of dependencies in mix.exs:
def deps do
[
{:phoenix_view_assets, "~> 0.1.0"}
]
end
Use (or merge) example_assets for assets/ in your project.
Setup
- Create module
MyApp.Assets:
defmodule MyApp.Assets do
use PhoenixViewAssets
end
- Use
MyAppWeb.Assetsin your layout view to generatescriptsandstylesfunctions in compile time.
defmodule MyApp.LayoutView do
use MyAppWeb, :view
use MyAppWeb.Assets
end
- Use
stylesandscriptsfunctions fromMyApp.Assetsto add assets to your layout:
<head>
...
<%= for path <- styles(@conn) do %>
<link rel="stylesheet" href="#{path}" />
<% end %>
</head>
<body>
...
<%= for path <- scripts(@conn) do %>
<script type="text/javascript" src="#{path}" />
<% end %>
</body>