Skip to content

Commit

Permalink
[docs] add code example on how to render function components in contr…
Browse files Browse the repository at this point in the history
…oller (#5954)
  • Loading branch information
jatins authored Oct 22, 2024
1 parent a2e7de1 commit acd8561
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions guides/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,30 @@ Next we need to update `show.html.heex`:
</section>
```

When we reload `http://localhost:4000/hello/Frank`, we should see the same content as before.

Since templates are embedded inside the `HelloHTML` module, we were able to invoke the view function simply as `<.greet messenger="..." />`.

If the component was defined elsewhere, we can also type `<HelloWeb.HelloHTML.greet messenger="..." />`.
When we reload `http://localhost:4000/hello/Frank`, we should see the same content as before. Since the `show.html.heex` template is embedded within the `HelloHTML` module, we were able to invoke the function component directly as `<.greet messenger="..." />`. If the component was defined elsewhere, we would need to give its full name: `<HelloWeb.HelloHTML.greet messenger="..." />`.

By declaring attributes as required, Phoenix will warn at compile time if we call the `<.greet />` component without passing attributes. If an attribute is optional, you can specify the `:default` option with a value:

```
attr :messenger, :string, default: nil
```

Although this is a quick example, it shows the different roles function components play in Phoenix:

* Function components can be defined as functions that receive `assigns` as argument and call the `~H` sigil, as we did in `greet/1`

* Function components can be embedded from template files, that's how we load `show.html.heex` into `HelloWeb.HelloHTML`
Overall, function components are the essential building block of Phoenix rendering stack. The majority of the times, they are functions that receive a single argument called `assigns` and call the `~H` sigil, as we did in `greet/1`. They can also be invoked from templates, with compile-time validation of its attributes declared via `attr`.

* Function components can declare which attributes are expected, which are validated at compilation time
In fact, every template embedded into `HelloHTML` is a function component in itself. `show.html.heex` simply becomes a function component named `show`. This also means you can directly render function components directly from the controller, skipping the `show.html.heex` template:

* Function components can be directly rendered from controllers
```elixir
def HelloWeb.HelloController do
use HelloWeb, :controller

* Function components can be directly rendered from other function components, as we called `<.greet messenger={@messenger} />` from `show.html.heex`
def show(conn, %{"messenger" => messenger}) do
# Render the HelloWeb.HelloHTML.greet/1 component
render(conn, :greet, messenger: messenger)
end
end
```

And there's more. Before we go deeper, let's fully understand the expressive power behind the HEEx template language.
Next, let's fully understand the expressive power behind the HEEx template language.

## HEEx

Expand Down

0 comments on commit acd8561

Please sign in to comment.