app/lib/app_web/controllers
It’s in the controllers folder.
In Phoenix, views are different. The views are broken down into two things:
- The view_html.ex
- and the folder.
Example:
So I have artist_controller.ex
but for the view I have artist_html.ex
and the folder artist_html
.
Both of these, the artist_html.ex
and the folder artist_html
are in the controllers folder. That’s just how phoenix defaults it.
Within the folder artist_html
is corresponding files that map to route like show
and index
.
defmodule GiraWeb.ArtistHTML do
@moduledoc """
This module contains pages rendered by ArtistController.
See the `artist_html` directory for all templates available.
"""
use GiraWeb, :html
embed_templates "artist_html/*"
The artist_html.ex
also have a line to refer to the folder. GiraWeb is my app name btw.
Router:
defmodule GiraWeb.Router do
use GiraWeb, :router
...
scope "/", GiraWeb do
pipe_through :browser
get "/artists", ArtistController, :index
get "/artists/:page", ArtistController, :index
get "/artist/:artist/:id", ArtistController, :show
...
controller:
defmodule GiraWeb.ArtistController do
use GiraWeb, :controller
alias Gira.Novel.Contexts.Novels.NovelContext
alias Gira.Novel.Contexts.Artists.ArtistContext
def index(conn, params) do
artists = ArtistContext.list_artists_paginate(50, params)
page_title = "Artist Listing"
render(conn, :index, artists: artists, page_title: page_title)
end
def show(conn, %{"id" => id}) do
artist = id |> ArtistContext.read_artist!()
novels = id |> NovelContext.list_novels_by_artist_id()
page_title = "Artist: " <> artist.artist_name
render(conn, :show, artist: artist, novels: novels, page_title: page_title)
end
end
artist_html.ex
defmodule GiraWeb.ArtistHTML do
@moduledoc """
This module contains pages rendered by ArtistController.
See the `artist_html` directory for all templates available.
"""
use GiraWeb, :html
embed_templates "artist_html/*"
def table_artist(assigns) do
~H"""
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr class="bg-gray-200">
<th scope="col" class="px-6 py-3">
<div class="flex items-center">
Artist
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="hover:bg-gray-100 odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800 border-b dark:border-gray-700 cursor-pointer" :for={art <- @artists}>
<td style="scroll-margin-top: 69px" class="px-6 py-4" id={to_string(art.artist_name)}>
<.link class="hover:text-violet-200 hover:font-medium hover:underline" href={~p"/artist/#{art.artist_name}/#{art.id}"} title={art.artist_name}>
<%= art.artist_name %>
</.link>
</td>
</tr>
</tbody>
</table>
</div>
"""
end
end
artist_html/index.html.heex
<div class="flex flex-row flex-wrap overflow-x-auto">
<div id="side_1" class="w-1/5 pl-5">
</div>
<div id="main" class="w-3/5 pl-5">
<.table_artist artists={@artists} />
<.paginate paginate={@artists} route="artists" />
</div>
<div id="side_2" class="w-1/5 pr-5">
</div>
</div>
artist_html/show.html.heex
<div class="flex flex-row flex-wrap overflow-x-auto">
<div id="side_1" class="w-1/5 pl-5">
</div>
<div id="main" class="w-3/5 pl-5">
<h1 class="text-2xl">
<div class="py-5">Artist Name: <%= @artist.artist_name %></div>
</h1>
<.table_novel novels={@novels} />
</div>
<div id="side_2" class="w-1/5 pr-5">
</div>
</div>