Anyone else excited for the upcoming changes in Phoenix 1.7?
Looks like phoenix_view
is getting deprecated in favour of phoenix_template
:
With Phoenix.LiveView, Phoenix.View has been replaced by Phoenix.Component. Phoenix.Component is capable of embedding templates on disk as functions components, using the embed_templates function. For example, in Phoenix v1.7+, the YourAppWeb.UserView above would be written as:
defmodule YourAppWeb.UserHTML do
use YourAppWeb, :html
embed_templates "users"
end
The benefit of Phoenix.Component is that it unifies the rendering of traditional request/response life cycles with the composable component model provided by LiveView.
Replace use MyApp, :view by use MyApp, :html and invoke embed_template “…/templates/my”. Alternatively, you can move both the HTML file and its templates to the controllers directory, to align with Phoenix v1.7 conventions.
More details here.
Not sure what exactly are the 1.7 conventions, but I’m guessing this is the recurring idea of colocating controller, view and templates.
I have a non-trivial app that uses Phoenix views (“dead views”; non-live views). I’ve adopted the colocating approach and recently did full migration to Phoenix.Component
-based views. My view
macro is basically this:
def view do
quote do
use Phoenix.View,
root: "lib/myapp_web",
namespace: MyAppWeb
# x- prefix for Alpine.js
use Phoenix.Component, global_prefixes: ["x-"]
import MyAppWeb.Components
import Heroicons.LiveView
# ...
end
end
I believe that Phoenix is really pushing the boundaries of modern web apps, including traditional request-response ones. Amazing stuff