# Layouts & Menus

## Layouts

### Sidebar layout

This is the typical web application layout. It allows for a large number of menu items on the sidebar.

```html
<.layout
  current_page={@current}
  type="sidebar"
  current_user_name={user_name(@current_user)}
  main_menu_items={main_menu_items(@current_user)}
  user_menu_items={user_menu_items(@current_user)}
  home_path={home_path(@current_user)}
>
  <.container max_width="xl">
    <div>content</div>
  </.container>
</.layout>e
```

![Sidebar layout in action](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2F5TqOE7ZHCWyU3d3RtkxQ%2FScreen%20Shot%202022-03-04%20at%2012.46.34%20pm.png?alt=media\&token=dce80504-be8d-4c56-aef0-1bf6c9b6e444)

![Sidebar layout on mobile (closed)](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2FdOZ7LdGVhO0gG2dUGavj%2Fsidebar_mobile_closed.png?alt=media\&token=ff70d599-9d0e-4f11-b228-d861fd64527d)

![Sidebar layout on mobile (open)](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2Fm6kDFm6sE7AD9pvORUoq%2Fsidebar_mobile_open.png?alt=media\&token=b8778309-dced-47d6-9074-ae2abf782ce1)

### Stacked layout

A stacked layout with a navbar and then content.

```html
<.layout
  current_page={@current}
  type="stacked"
  current_user_name={user_name(@current_user)}
  main_menu_items={main_menu_items(@current_user)}
  user_menu_items={user_menu_items(@current_user)}
  home_path={home_path(@current_user)}
>
  <.container max_width="xl">
    <div>content</div>
  </.container>
</.layout>e
```

![Stacked layout](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2FeTKBupSXzvdnn7fSrelt%2Fstacked_layout.png?alt=media\&token=4bd896b7-1c1a-4563-8a84-251889dcd5a3)

![Stacked layout on mobile](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2FSSdC8bvKeKfiHDB4jppY%2Fstacked_mobile_closed.png?alt=media\&token=48eab621-3a83-4637-a2bd-66b472520b43)

![Stacked layout on mobile with menu open](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2FOyznaQ1CW14rX0kUAaez%2Fstacked_mobile_open.png?alt=media\&token=dcda0558-1be0-4cd8-a76a-b2d5201782d4)

## 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:

```elixir
def get_link(:register, _current_user) do
  %{
    name: :register,
    label: "Register",
    path: Routes.user_registration_path(Endpoint, :new),
    icon: :clipboard_list
  }
end
```

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.

```elixir
def get_link(:admin_users = name, current_user) do
  if Helpers.is_admin?(current_user) do
    %{
      name: name,
      label: "Users",
      path: Routes.admin_users_path(Endpoint, :index),
      icon: :users
    }
  else
    nil
  end
end
```

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](https://50448613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSwy36CS0dsnIH4ah9iHk%2Fuploads%2FqOXntSSrIj7Klg2L4Bcx%2FMenus.png?alt=media\&token=f0f376ad-d4db-405d-b5d4-47fb314b09e9)
