Adding processed styles from stylesheet to HTML template

If you load the contents into a module attribute, it’s read from the filesystem only once during the compilation.

Oh, I misunderstood you. Ignore me.


I’d put it in a function instead of a module attribute though. This way you would be able to invoke it from other modules as well, not just within the module where you defined the module attribute.

defmodule Web.CSSView do
  app_css = File.read!(Path.join(:code.priv_dir(:your_app_name), "static/css/app.css"))
  def inline_css(:app) do
    {:safe, unquote(app_css)}
  end
end

and then in any template

<style type="text/css"><%= Web.CSSView.inline_css(:app) %></style>