Confused by :rest and global attributes

I’m confused by the discussion of the global attribute discussion at https://hexdocs.pm/phoenix_live_view/Phoenix.Component.html#module-attributes. The following example is given:

attr :message, :string, required: true
attr :rest, :global

def notification(assigns) do
  ~H"""
  <span {@rest}><%= @message %></span>
  """
end

An example of the use of the function component is then given:

<.notification message="You've got mail!" class="bg-green-200" phx-click="close" />

Which would be rendered as:

<span class="bg-green-200" phx-click="close">You've got mail!</span>

Will the symbol :rest always be the designated global attribute? I suspect not.

Would someone mind giving just a bit more explanation and possibly an example where something other than :rest is given as a global attribute?

:rest is just the conventional name that has been chosen. It is not special, and it is also not exposed outside of the component.

The given example could have been:

attr :message, :string, required: true
attr :other, :global

def notification(assigns) do
  ~H"""
  <span {@other}><%= @message %></span>
  """
end

rest is used throughout Phoenix.Component so you might be swimming upstream if you wanted to attempt use something else.

Can’t imagine why you would need to use another name? It’s not like you can have more than one of them…

I was just wondering whether it was a reserved word. Thanks

1 Like