Is there a best practice for precomputing assigns outside render


Probably missing something obvious but…

I want to generate some helper information for ease of rendering. In the docs it recommends “Instead explicitly precompute the assign in your LiveView, outside of render:”

Where should I do this? Do I have to do it in every handle_* function with assign/2 (seems like a lot of duplication) or can I do it at the top of render/1 with a single, explicit assigns = Map.put(assigns, ...). I haven’t seen the later done anywhere, it seems to work but am I building trouble for later?



defp computed_assigns(socket) do

… and just wrap socket in each return value of callbacks.

1 Like