The user wants a "boilerplate" for an Elixir Phoenix web application with LiveView.
This is a common request because setting up Phoenix can involve several steps, and a "boilerplate" implies a ready-to-go structure with common configurations already in place.
However, simply dumping code isn't enough. A Phoenix boilerplate is essentially the output of the phx.new generator, but usually, people want more than just the bare minimum. They often want:
Authentication: phx.gen.auth is standard now.
Tailwind CSS: Standard in newer versions (1.7+).
Basic Project Structure: Explaining the folders.
Dependencies: Standard mix.exs.
I cannot generate a downloadable zip file directly. I must provide the commands to generate the boilerplate and then suggest code modifications or file structures to make it a "best practice" starting point.
Plan:
Prerequisites: Briefly mention Elixir, Erlang, and Hex.
Generation Commands:
Standard mix phx.new command.
Including the --live flag (though it's default now, it's good to be explicit or aware of history).
Database setup (Postgres is standard).
Project Structure Overview: Explain what the generator creates (lib/app, lib/app_web, etc.).
Core Components (The "Boilerplate" Value Add):
Router: Basic pipeline.
Application.ex: The Supervisor tree.
Endpoint.ex: Configuration.
Layouts: Core UI structure (Root vs App layouts).
Adding Authentication (Crucial for most apps):
Command mix phx.gen.auth.
LiveView Specifics:
A sample LiveView component or page to demonstrate how it works.
Step 5: Code Templates.
I will provide the key files needed to understand the setup:
mix.exs (Dependencies).
lib/app_web/router.ex (Routes).
lib/app_web/live/page_live.ex (A basic LiveView).
lib/app/application.ex (Supervision tree).
Step 6: Run it.mix ecto.setup, mix phx.server.
Refining the "Boilerplate" aspect:
A raw phx.new output is technically a boilerplate. To make this answer better, I should provide a configured boilerplate structure.
I will create a minimal example that includes:
Standard setup.
A basic LiveView counter or page to verify LiveView is working.
Tailwind setup (implied by modern Phoenix).
Let's structure the response as a guide to generating the boilerplate, followed by the essential code blocks that define it.
Self-Correction during thought process:
The user might not have Postgres installed. I should mention SQLite as an alternative (--database sqlite3) for quick prototyping, but stick to Postgres as the "standard" boilerplate.
I will present the solution in two parts:
The Fast Way: The CLI commands to generate the industry-standard boilerplate.
The Anatomy: A look at the specific files that make up the boilerplate (Router, LiveView module, Endpoint).
Detailed Code Snippets to include:
router.ex: Show the live "/" route.
page_live.ex: Show mount and render.
core_components.ex: Mention this is where Tailwind/HTML components live in Phoenix 1.7+.
Let's stick to Phoenix 1.7+ syntax (verified, verified_routes, etc.).
Final Output Structure:
Prerequisites check.
The Magic Command (Generator).
Adding Auth (Standard boilerplate requirement).
Directory Structure explanation.
Key File Examples (The "Manual" Boilerplate).
Here is a complete guide to generating a modern Phoenix LiveView boilerplate.
Since Phoenix 1.7+, LiveView is the default. The modern "boilerplate" also includes Tailwind CSS for styling and esbuild for assets, eliminating the need for complex Webpack configurations.
1. Prerequisites
Ensure you have installed:
Elixir (v1.14+ recommended)
Erlang/OTP (v24+)
Phoenix Archive:
bash
mix archive.install hex phx_new
PostgreSQL (Standard for Phoenix) or Docker.
2. The Generation Command
Run the following command in your terminal. We will call the app my_app.
bash
mix phx.new my_app --live
# Note: As of Phoenix 1.7, --live is the default, but adding it ensures compatibility.
When asked to install dependencies, type Y and press Enter.
3. Database Setup
By default, Phoenix expects a Postgres user with the password "postgres".
Navigate into the project:
bash
cd my_app
Create the database:
bash
mix ecto.setup
(If your Postgres credentials differ, edit config/dev.exs before running this).
4. Start the Server
bash
mix phx.server
Open http://localhost:4000. You will see the standard Phoenix landing page, which is now rendered entirely via LiveView.
5. The "Boilerplate" Anatomy
Here is the specific code structure of the generated boilerplate that you need to know for building apps.
A. The Router (lib/my_app_web/router.ex)
This defines how requests are handled. Phoenix 1.7 uses live_session to group LiveViews.
elixir
defmodule MyAppWeb.Router do
use MyAppWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, html: {MyAppWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
end
scope "/", MyAppWeb do
pipe_through :browser
# The Home LiveView
live "/", HomeLive, :index
end
end
B. The Core Layout (lib/my_app_web/components/layouts.ex)
In modern Phoenix, layouts are Function Components, not templates.
elixir
defmodule MyAppWeb.Layouts do
use MyAppWeb, :html
embed_templates "layouts/*"
# This adds the header/nav to every page
def app(assigns) do
~H"""
<header>
<.link navigate="/">Home</.link>
</header>
<main>
<.flash_group flash={@flash} />
<%= @inner_content %>
</main>
"""
end
end
C. A Basic LiveView Module (lib/my_app_web/live/home_live.ex)
This is the boilerplate for a stateful interactive page.
elixir
defmodule MyAppWeb.HomeLive do
use MyAppWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, count: 0)}
end
@impl true
def render(assigns) do
~H"""
<div class="text-center">
<h1 class="text-4xl font-bold">Welcome to Phoenix!</h1>
<p>Count: <%= @count %></p>
<button phx-click="inc" class="bg-blue-500 text-white px-4 py-2 rounded">
+
</button>
</div>
"""
end
@impl true
def handle_event("inc", _, socket) do
{:noreply, update(socket, :count, &(&1 + 1))}
end
end
6. Recommended: Add Authentication
Most boilerplates need Auth. Phoenix has a built-in generator for this that creates a fully functional LiveView-based authentication system.
Run the generator:
bash
mix phx.gen.auth Accounts User users
Follow the instructions in the terminal (usually migrating the DB).
This automatically injects on_mount: [...] hooks into your router.ex to protect routes.
7. Recommended: Add a Component Library
To make development faster, consider adding Heroicons or Petal Components.
Add heroicons to your mix.exs:
elixir
defp deps do
[
{:heroicons, "~> 0.5"}
]
end
Then run mix deps.get. You can now use icons in your LiveViews:
elixir
<Heroicons.home class="w-6 h-6" />
Summary
Your lib/ folder structure should look like this:
text
my_app/
├── lib/
│ ├── my_app/ # Business Logic (Contexts, Schemas)
│ ├── my_app_web/ # Web Layer
│ │ ├── components/ # Shared UI components (Layouts, Core)
│ │ ├── controllers/ # Traditional controllers (rarely used now)
│ │ ├── live/ # <-- YOUR LIVEVIEWS GO HERE
│ │ └── router.ex
│ └── my_app.application.ex
└── assets/ # CSS/JS (Tailwind/ESBuild)