0

I am trying to create a menu in Phoenix that has a list based on a database query.

## app.html.heex
<div>
  <ul> ## @items = MyApp.Items.Item.list_items
     <%= for item <- @items do %>
     <li><%= item.name %></li>
  <% end %>
 ...

The menu is used for both live and nonlive pages. I would like to put it in a controller so that it will be called once and not every time a use changes controllers.

How can I make a controller for root.html.heex or app.html.heex for this?

Jun Dalisay
  • 1,165
  • 2
  • 12
  • 26

2 Answers2

0

To create a controller for your menu in Phoenix that will be called once and not every time a user changes controllers, you can use a LiveView. LiveViews in Phoenix are designed to handle real-time, interactive updates on the web page without reloading the entire page.

$ mix phx.gen.live Menu Menu

This command will generate the necessary files for your LiveView, including the controller, view, and template.

S dh
  • 1
0

My solution was to use a different root.html.heex template for each Controller, using the router.ex:

pipeline :menu do
  plug :put_root_layout, {MyAppWeb.LayoutView, :items}
end

Then create items.html.heex which renders the menu list from whatever controller

Jun Dalisay
  • 1,165
  • 2
  • 12
  • 26