annad

annad

Documentation for adding fonts to Liveview Project

I am using Tailwinds and I want to use a font family I found on Google. I have a url for the font, but I think it would be safer to download and add the font to my project. I can’t find any documentation (on the Phoenix site) on how to do this. I have found some previous Elixir Forum posts, but they are old posts and don’t know if they apply to 1.5.

Could someone point me in the right direction?

Most Liked

kokolegorille

kokolegorille

I made a small script to retrieve fonts and have them locally…

#!/bin/sh

for family in $*; do
for url in $( {
for agent in \
'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0' \
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' \
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)' ;
do
curl -A "$agent" -s "http://fonts.googleapis.com/css?family=$family" | \
grep -oE 'http://[A-Za-z0-9/._-]+'; \
done } | sort -u ) ;
do
extn=${url##*.} ;
file=$(echo "$family"| tr +[:upper:] _[:lower:]);
echo $url $file.$extn;
curl -s "$url" -o "$file.$extn";
done
done

For example.

$ ./get_font.sh Roboto
http://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxM.woff roboto.woff
http://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxPKTU1Kg.ttf roboto.ttf

Usually I put them in assets/fonts and have them processed by webpack, and file loader.

Then I just add a font stylesheet… like this.

@font-face {
  font-family: roboto;
  src: url("../fonts/roboto.ttf") format("truetype"); /* For IE */
  src: local("roboto"), url("../fonts/roboto.ttf") format("truetype"); /* For non-IE */

  src: local("roboto"), url("../fonts/roboto.woff") format("woff");
}

and the webpack rule.

        // Load fonts
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[name].[ext]",
                // Relative to output public_path
                outputPath: "./fonts/"
              }
            }
          ]
        },

You need to install file_loader npm package to do this…

malloryerik

malloryerik

OK, so, older versions of Phoenix used Brunch, but then switched to Webpack. So Phoenix currently uses Webpack and not Brunch.

We can divide your fonts issue into two parts:

  1. getting the fonts into your application, and
  2. using them.

For the first part, since you’re using Google Fonts, the easiest way to get the fonts into your app is probably just to use the Google Fonts cdn link from fonts.google.com, something like this:

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Merriweather&family=Oswald&display=swap" rel="stylesheet">

You put that into the head of your root.html.leex file, so that’s in lib/myapp_web/templates/layout/root.html.leex

Just add the links in the head of that file, so somewhere <head> here </head>.

That’s what @kokolegorille meant when they said, “link the stylesheet in root.html.eex.”

Once that’s done you can get the second part right, making sure that you’re using the fonts correctly within your app.

Then when you are going to deploy or have a moment, you can work on a more performant way of getting the fonts installed in your app, like the way @kokolegorille code showed earlier, or just using the Fontsource way.

annad

annad

YAY!!! The links worked! Thank you for dividing the issue in two parts and telling me where to put those links. Sometimes it is so embarrassing the basic things I don’t know. Sigh. I’m going to work on styling and then come back to this thread to investigate all of the options for “loading” the fonts for deployment. I will definitely check out @fontsource (thank you @c4710n for those links).

While it feels like I spent a week on fonts, it was really a valuable lesson on static assets. Thank you!

APB9785

APB9785

Creator of ECSx

You should be able to just replace the URL with a relative path name like

src: url('/fonts/my-font.ttf');
kokolegorille

kokolegorille

You can have a look at the patterns for live_reload in your config/dev.exs file. It defines when to reload. It looks like this.

    patterns: [
      ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
      ~r"priv/gettext/.*(po)$",
      ~r"lib/my_app_web/(live|views)/.*(ex)$",
      ~r"lib/my_app_web/templates/.*(eex)$",
    ]

Where Next?

Popular in Questions Top

Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement