在 Phoenix 中使用自定义字体

Use Custom Fonts in Phoenix

我正在尝试在我的 Phoenix 应用程序中使用自定义字体。我已将它们放在 priv/static/fonts 目录中,并在 web/templates/layout/app.html.eex 模板中正确创建并加载了 css 文件,但它们未由 Phoenix 服务器提供服务。

/Users/Psycho/code/elixir/my_app/
▾ priv/
  ▸ repo/
  ▾ static/
    ▸ css/
    ▾ fonts/
      ▾ walsheim/
          gt-walsheim-light-web.svg
          gt-walsheim-light-web.eot
          gt-walsheim-light-web.ttf
          gt-walsheim-light-web.woff

用于获取字体的 css 文件:

// my_app/priv/css/fonts.css

@font-face {
    font-family: "Walsheim";
    font-style: normal;
    font-weight: 300;
    src:
        url("/fonts/walsheim/gt-walsheim-light-web.eot?#iefix") format("embedded-opentype"),
        url("/fonts/walsheim/gt-walsheim-light-web.woff") format("woff"),
        url("/fonts/walsheim/gt-walsheim-light-web.ttf") format("truetype"),
        url("/fonts/walsheim/gt-walsheim-light-web.svg#Walsheim") format("svg");
}

好的,找到解决方案了。

看来您必须告诉 phoenix 为静态文件提供哪些目录。我进入我的 my_app/lib/my_app/endpoint.ex 文件并更新了 Plug.Static 插件以服务于 fonts 文件夹:

defmodule MyApp.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  plug Plug.Static,
    at: "/", from: :my_app, gzip: false,
    only: ~w(css images js fonts favicon.ico robots.txt)

  # Other Stuff ...
end

来源:PhoenixTalk - Serving static assets in a Sub-Folder other than the defaults