sodapopcan

sodapopcan

Dynamically render Surface components

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.

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!

First Post!

sodapopcan

sodapopcan

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

Essentially:

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

Last Post!

sodapopcan

sodapopcan

Nice. Thanks for the link!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement