Changing directory structure and module names

Phoenix compiles the templates into the view code, so it needs to find them at compile time. You can dynamically render any view from a controller, but in most cases you’ll have a dedicated one per controller, so to avoid the boilerplate there is a naming convention for linking the controller with the view. This implies how route helpers infer the function names.

I think you should be able to mostly configure things the way you want.

I personally group the code by feature but keep the naming scheme, which requires minimal config changes:

- lib
  - my_app_web
    - password_reset
      - password_reset_controller.ex (MyAppWeb.PasswordResetController)
      - password_reset_view (MyAppWeb.PasswordResetView)
      - new.html.eex

and then in my_app_web:

defmodule MyAppWeb do
  def view do
    quote do
      use Phoenix.View,
        root: "lib/my_app_web",
        # instead of root "lib/my_app_web/templates"
        namespace: MyAppWeb
  end
end

See the Phoenix.View docs.

8 Likes