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. 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 HTML blocks to function components, as to regular HTML tags. For example, you could create a button component that is invoked like this: <.button> This does inside the button! Where the function component would be defined as: def button(assigns) do ~H"\"" "\"" end Where `render_block` is defined at `Phoenix.LiveView.Helpers.render_block/2`. """ @doc false defmacro __using__(_) do quote do import Phoenix.LiveView import Phoenix.LiveView.Helpers end end end