Athunnea

Athunnea

LiveView functional components code style: slots or attributes?

I have been playing with Functional Components from LiveView 0.17. In some cases you can achieve the same result using either slots or attributes. What do you think works better in the following cases?

Using attributes

<Components.badge text={@text} />
<Components.description_list elements={
  [
    {gettext("Legal Name"), @company.legal_name },
    {gettext("Country"),    @company.country    },
    {gettext("Address"),    @company.address    },
  ]
} />

Using slots

<Components.badge><%= @text %></Components.badge>
<Components.attributes>
  <:attribute label={gettext("Legal Name")}><%= @company.legal_name %></:attribute>
  <:attribute label={gettext("Country")}>   <%= @company.country    %></:attribute>
  <:attribute label={gettext("Address")}>   <%= @company.address    %></:attribute>
</Components.attributes>

This is for text values only. For html blocks I’d use slots.

It seems that using attributes is a bit cleaner, less visual noise. Maybe that is because I personally hate <%= ... %> ever since I started using Rails ages ago. It’s noisy and takes some effort to type. {...} is so much cleaner. On the other hand, using attributes for content feels a bit wrong.

So, attributes or slots?

  • Attributes obviously, it’s cleaner
  • Keep attributes for attributes, use slots for content
0 voters

First Post!

sodapopcan

sodapopcan

I would say instead of either of them, provide a Components.description_item component. If you are building a library and want to give people options, I would make both the elements attribute and description_item available. Either way, I wouldn’t use slots for this.

Where Next?

Popular in Discussions Top

New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement