Hey there! Tonight I was feverishly working on a basic Phoenix 1.7.0 app that lets me store various links. The goal was to build out a site that would replace having to keep a whole bunch of tabs open in my laptop and mobile browsers. Here it is: https://loulinks.net. Enjoy!
This is a continuation of Part 1, which you can find here:
Now, let’s navigate to the
index.html.heexfile, which is located in
The first thing we encounter is something that looks like an HTML tag, but is actually a LiveView function component. We can tell that it is a function component because it begins with a
.. You can find the definition of the header component in
lib/todo_web/components/core_components.ex. We can see that it has a class attr, an inner_block, a subtitle, and associated actions slots.
:actions, we see that there is a
.link. This is defined in LiveView itself (see:
deps/phoenix_live_view/lib/phoenix_component.ex— search for “
def link“. Feel free to also read the documentation at the top of the file). You can find the documentation for the
patchattr in the file listed above (search for
"attr.(:patch"). The long and short of it is that
/todo_items/newis first translated into a Verified Route (note the
~psigil), which then will call
Next, we have a
.table. You can also view this function component in
lib/todo_web/components/core_components.ex. Basically, the table function component has three attrs (
rows) and a slot
colwhich takes an optional attr
labelplus a slot
rowsattr passes each list item into the
:letslot of the associated slot, and the
JS.navigate, which will, in turn navigate the page to the
show.exLiveView. You can learn more about the
JSmodule here: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.JS.html
Next, we have the
modalfunction component, which is displayed only when the
[:new, :edit]. Basically, it renders a LiveComponent, which you can find in
lib/todo_web/live/todo_item_live/form_component.ex. You can read more about LiveView’s LiveComponent’s here: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html
And that’s it! Now we navigated through
index.html.heex, so you should have a better understanding of function components and where you can find the docs for them.
Again, if you gained value from this article, feel free to throw me a dollar or two on my Buy Me a Coffee page, which you can find here: https://www.buymeacoffee.com/danieljaouen
Thanks for reading!
I will assume that you have Elixir 1.14 installed. We are going to be using Phoenix version 1.7 in this tutorial. To install it, run the following command in your terminal:
mix archive.install hex phx_new
You may need to uninstall your previous version first (if you had it installed already):
mix archive.uninstall phx_new
Creating the App
Great! Now, let’s begin our Phoenix project by using the
mix phx.new todo
Great! Now let’s
cdinto the new
tododirectory and run the following commands:
Great! That created the initial PostgreSQL database for us. Now, let’s run the following command to make sure everything was set up correctly:
iex -S mix phx.server
Now browse to
localhost:4000and you should see the Phoenix welcome page. If not, you will need to debug the reason you are getting your specific error.
Creating the Todos
First, let’s take a look at the help dialog that ships with Phoenix by running the following command:
mix help phx.gen.live
Feel free to read this at your convenience. Now, let’s create the Todos using the following command:
mix phx.gen.live Items TodoItem todo_items text:string
Next, follow the instructions printed to your terminal about modifying your
Next, run the generated database migrations:
Now restart the Phoenix server and re-navigate to
localhost:4000/todo_itemsand you should see the listing page for your TodoItems. Great!
Navigating the Generated Code
Now that we have the Todo app up-and-running, let’s navigate through the code to get a glimpse at how things work under the hood.
First, let’s take a look at index.ex, the generated Elixir code for the index LiveView. We notice five functions:
mountis called when the client connects and is used to set up the initial
socket. You will notice that there is no
renderfunction, and that is because Phoenix uses a particular convention to display its templates: if there is an associated
index.ex, that template will be used.
handle_paramsis called whenever we have a live navigation event. These events are what you would normally have separate HTTP requests for. You can learn more about
handle_eventis called whenever we have a JS “delete” event pushed to the page. Finally,
list_todo_itemssimply calls the associated context’s function.
Join me next time when we dive into
index.html.heex. In the mean time, feel free to browse through the additional generated code.
If you gained value from this post, feel free to throw me a dollar or two on my Buy Me a Coffee page, which you can find here:
Your support goes to the cost of keeping the server up and is never unappreciated. Thanks!