πŸ“„
Layouts & Menus

Layouts

This is the typical web application layout. It allows for a large number of menu items on the sidebar.
1
<.layout
2
current_page={@current}
3
type="sidebar"
4
current_user_name={user_name(@current_user)}
5
main_menu_items={main_menu_items(@current_user)}
6
user_menu_items={user_menu_items(@current_user)}
7
home_path={home_path(@current_user)}
8
>
9
<.container max_width="xl">
10
<div>content</div>
11
</.container>
12
</.layout>e
Copied!
Sidebar layout in action
Sidebar layout on mobile (closed)
Sidebar layout on mobile (open)

Stacked layout

A stacked layout with a navbar and then content.
1
<.layout
2
current_page={@current}
3
type="stacked"
4
current_user_name={user_name(@current_user)}
5
main_menu_items={main_menu_items(@current_user)}
6
user_menu_items={user_menu_items(@current_user)}
7
home_path={home_path(@current_user)}
8
>
9
<.container max_width="xl">
10
<div>content</div>
11
</.container>
12
</.layout>e
Copied!
Stacked layout
Stacked layout on mobile
Stacked layout on mobile with menu open

Menus

In menus.ex there is a list of all the menu items. This can be useful when working with navbars and layouts, where you sometimes need to loop over menu items multiple times.
For all menu items, you must define a new get_link/2 function, which takes a name and current_user as the parameters. The name parameter is pattern matched and thus you statically include it in the function definition. A menu item has a name, label, path and icon. Here's an example of a menu item for the "Register" menu item:
1
def get_link(:register, _current_user) do
2
%{
3
name: :register,
4
label: "Register",
5
path: Routes.user_registration_path(Endpoint, :new),
6
icon: :clipboard_list
7
}
8
end
Copied!
You can use the current_user param to conditionally display a menu item. For example, here we don't show the menu item unless the user is an admin.
1
def get_link(:admin_users = name, current_user) do
2
if Helpers.is_admin?(current_user) do
3
%{
4
name: name,
5
label: "Users",
6
path: Routes.admin_users_path(Endpoint, :index),
7
icon: :users
8
}
9
else
10
nil
11
end
12
end
Copied!
You can build menu item lists to give to layouts. We define the two core menu lists in menus.ex:
How layouts use menu lists