📄Layouts & Menus
The layout component
Petal Pro gives the developer a <.layout>
component, which takes a type
attribute and renders a layout based on that type
. eg:
It works by a simple case statement:
You can maintain this file and add/remove layouts as you please.
How to modify an existing layout
Petal Pro provides a number of layouts: "sidebar", "stacked" and "public". These are quite configurable however you may want to modify them. To do so, simply duplicate the layout file from the _petal_framework/web/components/layout
folder into your /components
directory and change the module name. eg to modify the sidebar layout:
Copy
sidebar_layout.ex
to<your_app>_web/components/
Change the module name from
PetalFramework.Components.SidebarLayout
toYourAppWeb.Components.SidebarLayout
In
layout.ex
change theimport
statement to import your module instead of the old one.Now when you call
<.layout type="sidebar">
it will render your duplicate layout instead, and you can modify it to however you like
How to add a new layout
Create a new file in
<you_app>_web/components
, eg:my_cool_layout.ex
Look at what assigns other layouts like
sidebar.ex
use - you can see inlayout.ex
how menu items are passed in, as well as the user's name and avatarWhen complete, you can import your layout into
layout.ex
and modify thelayout/1
function - eg:
4. Now in your template or live view file, you simply update the type to your new layout:
Petal Pro Layouts
Sidebar layout
This is the typical web application layout. It allows for a large number of menu items on the sidebar.
Stacked layout
A stacked layout with a navbar and then content.
Public layout
Includes a header and footer - for use in public facing, marketing pages.
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:
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.
You can build menu item lists to give to layouts. We define the two core menu lists in menus.ex
:
Last updated