Today I Learned

hashrocket A Hashrocket project

Phoenix component attr definition

The new Phoenix.Component attr/3 function is awesome. It does compile time validations to all the places that are calling the component and also it comes with nice and useful options. For instance the default option:

attr :name, :string, default: "there"

def greet(assigns) do
  ~H"""
  <h1>Hey <%= @name %>!</h1>
  """
end

That's very useful. That would totally replace most usages of assign_new like I used to do:

def greet(assigns) do
  assigns = assign_new(assigns, :name, fn -> "there" end)

  ~H"""
  <h1>Hey <%= @name %>!</h1>
  """
end

This way we can call:

<.greet />

And have this html generated:

<h1>Hey there!</h1>
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.