nocksock

nocksock

Controller: Implicit rendering with heex

LiveView has spoiled me and now I would LOVE to be able to write controllers like this:

defmodule DeadViewWeb.ItemController do
  use DeadViewWeb, :controller
  alias DeadView.Inventory

  def new(conn, _) do
    conn = conn
      |> assign(changeset:  Inventory.change_item(%Inventory.Item{}))

   ~H"""
    <main>
      <.header>New Item</.header>
      <.item_form changeset={@changeset} action={~p"/items"} />
    </main>
   """ 
  end

  defp item_form(assigns) do
    ~H"""
    <.simple_form :let={f} for={@changeset} action={@action}>
      <.error :if={@changeset.action}>
        Oops, something went wrong! Please check the errors below.
      </.error>
      <.input field={f[:name]} type="text" label="Name" />
      <:actions>
        <.button>Save Item</.button>
      </:actions>
    </.simple_form>
    """
  end
end

I need to migrate a few forms back to controllers, and now the old way feels somewhat clunky, compared to LiveView, where I basically never use .html.heex files. And since controllers don’t need a mount callback with a separate render, this could be even more concise.

The conn = conn |> assign ... part isn’t great, I know - I’m sure there’s a better way.

From a mental model, this would be very similar to the direction remix, waku, Next.js and others in React-land are taking, ie: making it easier to pick up devs that have experience and appreciate these models[1], while also making the transition from Controllers to LiveView (and vice versa), given its similarities with RSC (to some extent).

It feels like something Chris McCord would be able to implement in a short lunchbreak with 20 LOC; so I’m wondering what the problems are with this and whether there’s prior work, before I explore this idea myself.


  1. I count myself somewhat part of that group. ↩︎

Marked As Solved

LostKobrakai

LostKobrakai

This is possible today:

def action(conn, _) do
  assigns = %{…}

  html(conn, Phoenix.HTML.Safe.to_iodata(~H"""
     …
  """))
end

Also Liked

nocksock

nocksock

I expected nothing less. :smiley:

Merely skimmed the page on rendering, but now saw that it mentions html/2. When I skimmed it, I was mostly looking at the code examples; and didn’t see any.

TIL! Thanks!

Last Post!

LostKobrakai

LostKobrakai

Phoenix.HTML.Safe.to_iodata does the conversion.

Where Next?

Popular in Proposals: Ideas Top

knoebber
I started a new phoenix project with --no-tailwind and found friction: delete tailwind classes from core components delete tailwind cla...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New
benkimpel
Background I work at a hedge fund and our traders need highly dynamic UIs (think splitters, tabbed panels, tree lists, enormous data grid...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
superchris
Hello! After seeing a couple of posts from Chris and Jose about supporting web components in LiveView, I thought it would be great to ini...
New
Astolfo
When generating a release with phx.gen.release --docker a debian version is used by default to avoid “DNS issues” on Alpine. It seems li...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement