Anko

Anko

Case statement in heex template

I have a bunch of components and a list of data with different “types”. I want to render the appropriate component depending on the type of the data.

This is basically the code i am trying, but i’m not sure the best way to do it with heex.

    for {agg, results} <- @data_list do
      case agg.type do
        "price_stats" -> ~H"""
          <.live_component module={ComponentTestWeb.Components.StatsFacet} id={agg.internal_name} title={agg.title} items={results} />
        """
        _ -> ~H"<.live_component module={ComponentTestWeb.Components.KeywordFacet} id={agg.internal_name} title={agg.title} items={results} />"
      end
    end

How should I do this? I have tried it in the template, but i couldn’t find a good example of case statements in the template. So then i thought i’d do it as a functional component, but it’s not clear to me how to set the assigns correctly. For example, this says i’m doing the wrong thing for change tracking;

  def show_facets(assigns) do
    
    for {agg, results} <- assigns.agg_results do
      case agg.type do
        "price_stats" -> ~H"""
          <.live_component module={ComponentTestWeb.Components.StatsFacet} id={agg.internal_name} title={agg.title} items={results} />
        """
        _ -> ~H"<.live_component module={ComponentTestWeb.Components.KeywordFacet} id={agg.internal_name} title={agg.title} items={results} />"
      end
    end
  end

Marked As Solved

LostKobrakai

LostKobrakai

I’d just put this 1:1 into heex and be fine. No need to make this more complicated unless it becomes unwieldy.

~H"""
<%= for {agg, results} <- @data_list do %>
  <%= case agg.type do %>
    <% "price_stats" -> %>
      <.live_component module={ComponentTestWeb.Components.StatsFacet} id={agg.internal_name} title={agg.title} items={results} />
    <% _ -> %>
      <.live_component module={ComponentTestWeb.Components.KeywordFacet} id={agg.internal_name} title={agg.title} items={results} />
  <% end %>
<% end %>
"""

If it’s however only the module changing you can also do this:

~H"""
<%= for {agg, results} <- @data_list do %>
    <.live_component module={component_by_type(agg.type)} id={agg.internal_name} title={agg.title} items={results} />
<% end %>
"""

defp component_by_type("price_stats"), do: ComponentTestWeb.Components.StatsFacet
defp component_by_type(_), do: ComponentTestWeb.Components.KeywordFacet
10
Post #4

Also Liked

kokolegorille

kokolegorille

Maybe like this (not tested)

alias ComponentTestWeb.Components.{StatsFacet, KeywordFacet}
def show_facets(assigns) do
  for {agg, results} <- assigns.agg_results do
    show_agg(agg, results)
  end
end

defp show_agg(%{type: "price_stats"} = agg, results) do
  assigns = %{agg: agg, results: results}
  ~H"""
    <.live_component module={StatsFacet} id={@agg.internal_name} title={@agg.title} items={@results} />
  """
end
defp show_agg(agg, results) do
  assigns = %{agg: agg, results: results}
  ~H"""
    <.live_component module={KeywordFacet} id={@agg.internal_name} title={@agg.title} items={@results} />
  """
end
sodapopcan

sodapopcan

I would do something like this (also untested):

alias ComponentTestWeb.Components.{StatsFacet, KeywordFacet}

def facets(assigns) do
  ~H"""
  <%= for {agg, result} <- @agg_results do %>
    <.agg_result agg={agg} result={result} />
  <% end %>
  """
end

defp agg_result(%{agg: %{type: "price_stats"}} = assigns) do
  ~H"""
  <.live_component module={StatsFacet} id={@agg.internal_name} title={@agg.title} items={@results} />
  """
end

defp agg_result(assigns) do
  ~H"""
  <.live_component module={KeywordFacet} id={@agg.internal_name} title={@agg.title} items={@results}
  """
end

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement