Liveview conventions, Why put liveview template in live folder?

Here’s how I structured the sign-in/sign-out (session management) and registration features:

$stefan:mbp ~/dev/myapp/lib/myapp_web/accounts > tree
.
├── registration
│   ├── confirmation.html.eex
│   ├── new.html.eex
│   ├── registration_controller.ex
│   └── registration_view.ex
├── session
│   ├── confirmation.html.eex
│   ├── new.html.eex
│   ├── session_controller.ex
│   └── session_view.ex
└── user_auth.ex

Sample controller:

defmodule MyAppWeb.Accounts.RegistrationController do
  use MyAppWeb, :controller
  ...
end

To make this work, you just need to tell Phoenix where to lookup the templates (myapp_web.ex):

  def view do
    quote do
      use Phoenix.View,
        root: "lib/myapp_web",
        namespace: MyAppWeb

With this setup, putting live view templates into the same directory as the live view is perfectly consistent with the rest of the app.

8 Likes