# Dynamically render Surface components

**URL:** <https://forum.elixirforum.com/t/dynamically-render-surface-components/38138>\
**Category:** Questions\
**Tags:** phoenix, liveview, surface\
**Created:** [March 12, 2021, 5:30pm UTC](https://forum.elixirforum.com/t/dynamically-render-surface-components/38138 "2021-03-12T17:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [March 12, 2021, 5:30pm UTC](https://forum.elixirforum.com/t/dynamically-render-surface-components/38138/1 "2021-03-12T17:30:30Z")

</div>

Is there a nice way to dynamically render a Surface component? I actually don’t even really figure out a non-nice way atm.

I current have a main `Page` which contains the layout and I want to dynamically render content which would be components.

```elixir
defmodule AppWeb do
  use Surface.LiveView

  data section, :string, default: "home"

  def render(assigns) do
    ~H"""
    <div>
      <header><h1>header</h1></header>
      <main>
        <!-- Is it possible to render a component based on the `section` data attribute here? -->
      </main>
    </div>
    """
  end

  def handle_params(%{"section" => section}, _, socket} do
    # Do some validation here to ensure `section` section is valid

    {:noreply, assign(:section, section)}
  end
end

```

Is there a better way of dong this? My first attempt involved having a `Layout` component with a slot, then each section is its own LiveView, but that felt a little heavy-handed. Each section is rich enough that I want them to be their own components, but not heavy enough that they warrant loading a new LiveView each time.

I feel like I’m missing something really obvious, but my focus is being pulled in all different directions in the past couple of weeks.

Thanks!

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [March 12, 2021, 6:42pm UTC](https://forum.elixirforum.com/t/dynamically-render-surface-components/38138/2 "2021-03-12T18:42:35Z")

</div>

So, reading through the source of LiveDashboard I’ve come up with something.

Essentially:

```elixir
defmodule AppWeb do
  use Surface.LiveView

  data page, :string

  def render(assigns) do
    ~H"""
    <div>
      <header><h1>header</h1></header>
      <main>
        {{ @page.render(assigns) }}
      </main>
    </div>
    """
  end

  def handle_params(%{"page" => page}, _, socket} do
    {:noreply, assign(:page, resolve_page(page))}
  end

  def handle_params(_, _, socket) do
    {:noreply, assign(:page, AppWeb.Pages.Home)}
  end

  defp resolve_page("foo"), do: AppWeb.Pages.Foo
  defp resolve_page("bar"), do: AppWeb.Pages.Bar
end

```

---

<div class="post-metadata">

**Author:** ![harmon25](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harmon25/32/1274_2.png) [@harmon25](https://forum.elixirforum.com/u/harmon25)\
**Post date:** [March 12, 2021, 7:55pm UTC](https://forum.elixirforum.com/t/dynamically-render-surface-components/38138/3 "2021-03-12T19:55:20Z")

</div>

[https://github.com/surface-ui/surface/issues/24#issuecomment-580049400](https://github.com/surface-ui/surface/issues/24#issuecomment-580049400)

Is how I am doing this.

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [March 12, 2021, 8:26pm UTC](https://forum.elixirforum.com/t/dynamically-render-surface-components/38138/4 "2021-03-12T20:26:21Z")

</div>

Nice. Thanks for the link!
