defmodule Phoenix.Component do @moduledoc """ API for function components. A function component is any function that receives an assigns map as argument and returns a rendered struct built with [the `~H` sigil](`Phoenix.LiveView.Helpers.sigil_H/2`).. Here is an example: defmodule MyComponent do use Phoenix.Component # Optionally also bring the HTML helpers # use Phoenix.HTML def greet(assigns) do ~H"\""
Hello, <%= assigns.name %>
"\"" end end The component can be invoked as a regular function: MyComponent.greet(%{name: "Jane"}) But it is typically invoked using the function component syntax from the `~H` sigil: ~H"\""Your name is: <%= @name %>
"\"" end However, when possible, it may be cleaner to break the logic over function calls instead of precomputed assigns: def show_name(assigns) do ~H"\""Your name is: <%= full_name(@first_name, @last_name) %>
"\"" end defp full_name(first_name, last_name), do: first_name <> last_name ## Blocks It is also possible to give HTML blocks to function components as in regular HTML tags. For example, you could create a button component that looks like this: def button(assigns) do ~H"\"" "\"" end and now you can invoke it as: <.button> This renders inside the button! In a nutshell, the block given to the component is assigned to `@inner_block` and then we use [`render_block`](`Phoenix.LiveView.Helpers.render_block/2`) to render it. You can even have the component give a value back to the caller, by using `let`. Imagine this component: def unordered_list(assigns) do ~H"\""