Difference between <%= and <. is heex-files

What is the difference between using <%= and <. in heex-files? After what I have understood, the second one is new to .heex-files and liveview, and I see it is used e.g. for live_components. Is there any differences, or are they interchangeable?

2 Likes

<%= @something %> is used to render @something on your HTML as long as it can become a string. You could change something to 1 + 1 or inspect(@current_user) and it would render something. <% and <%= are called tags in the docs for heex. I like to call them interpolation.

As for <.something> it means there’s a function on this module or imported in this module that’s a Phoenix.Component

These functions would be defined as:

def something(assigns) do
  ~H"""
   Some heex here
  """
end

They take an assigns argument and everything you pass as parameters go there. Like <.something x={10} /> populates the assigns with assigns.x with value 10. You can also use inside heex @x since it’s an assign.

Worth mentioning the “.” At the start means “it’s here or imported here”. If you wanted to call a Phoenix.Component function from another module you’d do: <Other.Module.function_name x={10} />. (Note there’s no leading dot).

9 Likes

Thank you for the clarifications!

1 Like