0

I'm trying to make use of the code in this blog post: [Phoenix LiveView] formatting date/time with local time zone.

I confirmed (with IO.inspect) that his mount and defp code I added to a LiveView adds the socket assigns. But he doesn't give an example of how to call the format_time function from a heex template. Perhaps it should be obvious, but it's not obvious to me. I tried this:

          <%= if connected?(@socket) do
                Cldr.format_time(@auction.started_or_paused_at,
                                 locale: @socket.assigns.locale,
                                 timezone: @socket.assigns.timezone)
              else
                Cldr.format_time(@auction.started_or_paused_at)
              end
          %>

But I get this error:

[error] GenServer #PID<0.655.0> terminating
** (KeyError) key :locale not found in: #Phoenix.LiveView.Socket.AssignsNotInSocket<>
    (ssauction_live 0.1.0) lib/ssauction_live_web/live/auction_live/show.html.heex:32: anonymous fn/2 in SSAuctionWeb.AuctionLive.Show.render/1
...

I guess I can't use the socket assigns while rendering?

Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173

1 Answers1

0

The socket assigns are available directly inside the template as LiveEEx assigns, such as @foo and @bar. Any assign access should be done using the assigns in the template where proper change tracking takes place.

Here are the official docs: Phoenix.LiveView.Socket.AssignsNotInSocketView

Amir Hussain
  • 139
  • 9