The user wants a boilerplate for an Elixir Phoenix web application with LiveView. They haven't specified a version, so I'll assume the latest Phoenix (1.7). I should provide a code skeleton and directory structure. While I can't reproduce the entire generator, I can focus on providing a concise template that includes key files like mix.exs, config files, and a router. I’ll mention how to generate the app with mix phx.new --live or how to manually incorporate LiveView. It’s about giving them a clear and functional start!Providing Phoenix boilerplate guidance
The user's question is a bit open-ended, and they didn’t specify if they want instructions or code, which makes it tricky. I’ll start by recommending the official generator and then present essential boilerplate elements: dependencies in mix.exs, a basic LiveView layout, and a router with a live route. I should mention that Phoenix 1.7 uses HEEx and has verified routes, providing sample code. It's safe to target 1.7 as the current version, but I’ll avoid claiming exact versions to stay accurate. I can show how to use mix phx.new for setup!Clarifying Phoenix 1.7 features
Phoenix 1.7 introduces the --install flag for handling assets, and you create a new app with mix phx.new my_app followed by mix setup, which sets up dependencies and prepares the project. For LiveView integration, the --live flag is used in the context of HTML projects, allowing you to include LiveView by default. Previous versions required the --live flag, but it’s safe to say that Phoenix 1.7 includes LiveView automatically. I'll instruct on how to install the latest release too!Creating a minimal LiveView app
The user is looking for a boilerplate, which means I should focus on providing a minimal example rather than installation instructions. I’ll create a simple Phoenix app that doesn’t use a database, focusing on key files like mix.exs, and the LiveView modules in lib/my_app_web/live/home_live.ex and lib/my_app_web/router.ex. For mix.exs, I'll include dependencies like {:phoenix, "> 1.7"} and {:phoenix_live_view, "> 0.20"} to keep it relevant. Adjustments may be necessary, but I’ll provide a solid starting point!Designing a minimal Phoenix LiveView boilerplate
The user is asking for a complete boilerplate, which I'll focus on by providing essential components for a minimal Phoenix LiveView app. I'll include import Phoenix.LiveView.Router in the router and set up the LiveView socket in the endpoint. I'll show mix.exs with the necessary dependencies, aiming for a concise structure without sacrificing clarity. Each file will be briefly defined: the application layout, router, LiveView components, and even optional asset setup. I'll keep explanations short while ensuring the skeleton compiles and stays useful for the user!Structuring a minimal Phoenix LiveView boilerplate
To fulfill the user's request, I'll emphasize providing clear instructions for creating a Phoenix LiveView app while including minimal code examples. First, I'll guide them to run mix phx.new my_app --live, which generates the necessary project structure. Next, I’ll outline essential components like a router with a live "/" route and an endpoint with a LiveView socket. I can create a basic PageLive module that demonstrates dynamic functionality, like a counter. I'll close with a brief mention of Ecto and the importance of following the generator’s defaults for completeness!Use the Phoenix project generator as the base and add a minimal LiveView. First, generate the project by running:
Create lib/my_app_web/live/home_live.ex to define your LiveView with a simple counter and layout. Finally, ensure your components, like headers, are set up properly in lib/my_app_web/components/core_components.ex. This will give you a solid foundation for developing your LiveView app! Use the Phoenix project generator as the base and then add a minimal LiveView. That’s the standard, battle‑tested boilerplate.
Below is:
How to generate the project.
The key files you need to have a clean Phoenix + LiveView boilerplate (with a simple counter).
1. Generate a Phoenix + LiveView project
Phoenix ≥ 1.7 includes LiveView by default.
bash
# Install/upgrade the project generator (one-time)
mix archive.install hex phx_new
# Create a new project (LiveView is on by default in 1.7+)
mix phx.new my_app
cd my_app
# Set up deps, assets, and dev DB (if you kept Ecto)
mix setup
# Start the dev server
mix phx.server
If you’re on Phoenix 1.6 or earlier, use:
bash
mix phx.new my_app --live
Everything below assumes the app name is MyApp and the OTP app is :my_app.
2. Router boilerplate (LiveView route)
lib/my_app_web/router.ex:
elixir
defmodule MyAppWeb.Router do
use MyAppWeb, :router
import Phoenix.LiveView.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
# LiveView home page
live "/", HomeLive, :index
end
# keep any API / other scopes you need
end
Most of that will already be present from mix phx.new; you usually just add the live "/" route (and import Phoenix.LiveView.Router if it’s not there already).
3. Endpoint boilerplate (LiveView socket)
lib/my_app_web/endpoint.ex (Phoenix already generates this; verify these bits are present):
The generator normally gives you all of this already; a LiveView‑ready app just needs the /live socket and Plug.Session configured.
4. Shared web layer (MyAppWeb)
lib/my_app_web.ex (trimmed to the parts you need for LiveView/HTML):
elixir
defmodule MyAppWeb do
@moduledoc """
Entry point for web interface: controllers, components, LiveViews, etc.
"""
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
def router do
quote do
use Phoenix.Router, helpers: false
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end
def controller do
quote do
use Phoenix.Controller,
formats: [:html, :json],
layouts: [html: MyAppWeb.Layouts]
import Plug.Conn
import MyAppWeb.Gettext
unquote(verified_routes())
end
end
def live_view do
quote do
use Phoenix.LiveView,
layout: {MyAppWeb.Layouts, :app}
unquote(html_helpers())
end
end
def live_component do
quote do
use Phoenix.LiveComponent
unquote(html_helpers())
end
end
def html do
quote do
use Phoenix.Component
unquote(html_helpers())
end
end
defp html_helpers do
quote do
import Phoenix.HTML
import Phoenix.Component
alias Phoenix.LiveView.JS
import MyAppWeb.Gettext
unquote(verified_routes())
end
end
defp verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: MyAppWeb.Endpoint,
router: MyAppWeb.Router,
statics: MyAppWeb.static_paths()
end
end
defmacro __using__(which) when is_atom(which),
do: apply(__MODULE__, which, [])
end
If you used mix phx.new you already have a very similar MyAppWeb module; keep that and use it instead of this if it’s present.
5. Layouts boilerplate
Phoenix 1.7 uses Layouts as a component module plus embedded templates.
lib/my_app_web/components/layouts.ex:
elixir
defmodule MyAppWeb.Layouts do
use MyAppWeb, :html
embed_templates "layouts/*"
end
defmodule MyAppWeb.CoreComponents do
use MyAppWeb, :html
# Simple header component used by LiveViews
attr :class, :string, default: nil
slot :inner_block, required: true
slot :subtitle
def header(assigns) do
~H"""
<header class={["border-b pb-4 mb-4", @class]}>
<h1 class="text-xl font-semibold">
<%= render_slot(@inner_block) %>
</h1>
<p :if={@subtitle != []} class="mt-2 text-sm text-gray-600">
<%= render_slot(@subtitle) %>
</p>
</header>
"""
end
end
The default Phoenix 1.7 generator will give you a richer CoreComponents; use that if it already exists.
7. A minimal LiveView (example boilerplate)
lib/my_app_web/live/home_live.ex:
elixir
defmodule MyAppWeb.HomeLive do
use MyAppWeb, :live_view
alias MyAppWeb.CoreComponents
def mount(_params, _session, socket) do
{:ok, assign(socket, page_title: "Home", count: 0)}
end
def handle_event("inc", _params, socket) do
{:noreply, update(socket, :count, &(&1 + 1))}
end
def handle_event("dec", _params, socket) do
{:noreply, update(socket, :count, &(&1 - 1))}
end
def render(assigns) do
~H"""
<.header>
Home
<:subtitle>A minimal Phoenix LiveView boilerplate</:subtitle>
</.header>
<section id="counter" class="space-x-2">
<p class="mb-2">Count: <%= @count %></p>
<button phx-click="dec" class="border px-2 py-1">-</button>
<button phx-click="inc" class="border px-2 py-1">+</button>
</section>
"""
end
end
This is a self‑contained LiveView with a simple counter, using your shared layout and components.
8. Minimal LiveView JS bootstrapping
assets/js/app.js (again, mix phx.new already creates something similar; use this if you’re starting from scratch):
javascript
// assets/js/app.js
import "phoenix_html"
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
let csrfToken = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken}
})
// Connect if any LiveViews are present
liveSocket.connect()
// Expose for debugging in the browser console
window.liveSocket = liveSocket
Make sure your mix.exs includes :phoenix, :phoenix_live_view, :phoenix_html, :plug_cowboy, etc. If you used mix phx.new, this is already handled.
9. Run it
From the project root:
bash
mix deps.get
mix ecto.create # if you kept Ecto and a DB
mix phx.server
Open http://localhost:4000 — you should see the LiveView at / with the counter buttons.
If you want, I can expand this boilerplate with authentication, a simple CRUD LiveView, or a DB‑backed example (e.g., a list and form with real‑time updates).