What do you expect as a result? HTML is just string information so you need to decide how to encode the elixir data you have into some string representation. It could be encoded in json/xml or whatever other string data format is out there.
I think you’re not completely aware of how live-view works. You cannot pass data you have at the time of rendering to the socket handling the updates. Those are completely different processes, which never communicate directly which each other. The only data the socket connection receives comes from the rendered html the client received (or from messages sent to the socket, but the socket does not yet exist at the time of rendering).
Hi, I’ve got the same problem, ended up with binary_to_term and base64.
Code look like this:
defmodule SomethingWeb.SomethingLive do
use Phoenix.LiveView
def render(assigns) do
~L"""
<div phx-click="test" phx-value="<%= encode(%{value: 1, test: "..."}) %>">ClickMe</div>
"""
end
def handle_event("test", data, socket) do
data = decode(data)
# data.value => 1
# data.test => ".."
{:noreply, socket}
end
defp encode(value) do
value
|> :erlang.term_to_binary()
|> Base.encode64()
end
defp decode(value) do
value
|> Base.decode64()
|> elem(1)
|> :erlang.binary_to_term()
end
end