This package includes some presenter classes that are used for converting menus to html. By default the generated menu style is bootstrap navbar
. But, there are also several different menu styles.
You can apply the menu style via ->style()
method.
Menu::create('navbar', function($menu) {
$menu->style('nav-pills');
});
Or you can set which presenter to present the menu style via ->setPresenter()
method.
Menu::create('navbar', function($menu) {
$menu->setPresenter(\Nwidart\Menus\Presenters\Bootstrap\NavTabPresenter::class);
});
You can also set which style of presenter when you rendering a menu.
Menu::render('navbar', 'navbar-right');
Menu::render('navbar', \Nwidart\Menus\Presenters\Bootstrap\NavPillsPresenter::class);
Style name | Presenter class |
---|---|
navbar | Nwidart\Menus\Presenters\Bootstrap\NavbarPresenter |
navbar-right | Nwidart\Menus\Presenters\Bootstrap\NavbarRightPresenter |
nav-pills | Nwidart\Menus\Presenters\Bootstrap\NavPillsPresenter |
nav-tab | Nwidart\Menus\Presenters\Bootstrap\NavTabPresenter |
sidebar | Nwidart\Menus\Presenters\Bootstrap\SidebarMenuPresenter |
navmenu | Nwidart\Menus\Presenters\Bootstrap\NavMenuPresenter |
You can create your own presenter classes. Make sure your presenter extends the Nwidart\Menus\Presenters\Presenter
and implements
the 'Nwidart\Menus\Presenters\PresenterInterface' interface.
For example, this is zurb-top-bar
presenter.
use Nwidart\Menus\Presenters\Presenter;
class ZurbTopBarPresenter extends Presenter
{
/**
* {@inheritdoc }
*/
public function getOpenTagWrapper()
{
return PHP_EOL . '<section class="top-bar-section">' . PHP_EOL;
}
/**
* {@inheritdoc }
*/
public function getCloseTagWrapper()
{
return PHP_EOL . '</section>' . PHP_EOL;
}
/**
* {@inheritdoc }
*/
public function getMenuWithoutDropdownWrapper($item)
{
return '<li'.$this->getActiveState($item).'><a href="'. $item->getUrl() .'">'.$item->getIcon().' '.$item->title.'</a></li>';
}
/**
* {@inheritdoc }
*/
public function getActiveState($item)
{
return \Request::is($item->getRequest()) ? ' class="active"' : null;
}
/**
* {@inheritdoc }
*/
public function getDividerWrapper()
{
return '<li class="divider"></li>';
}
/**
* {@inheritdoc }
*/
public function getMenuWithDropDownWrapper($item)
{
return '<li class="has-dropdown">
<a href="#">
'.$item->getIcon().' '.$item->title.'
</a>
<ul class="dropdown">
'.$this->getChildMenuItems($item).'
</ul>
</li>' . PHP_EOL;
;
}
}
To use this custom presenter, you can use the setPresenter
method.
Menu::create('zurb-top-bar', function($menu) {
$menu->setPresenter('ZurbTopBarPresenter');
});
Menu style is like an alias to a presenter. You can register your style from your costum presenter in the configuration file in config/menus.php
.
return [
'navbar' => 'Nwidart\Menus\Presenters\Bootstrap\NavbarPresenter',
'navbar-right' => 'Nwidart\Menus\Presenters\Bootstrap\NavbarRightPresenter',
'nav-pills' => 'Nwidart\Menus\Presenters\Bootstrap\NavPillsPresenter',
'nav-tab' => 'Nwidart\Menus\Presenters\Bootstrap\NavTabPresenter',
'zurb-top-bar' => 'ZurbTopBarPresenter',
];
Now, you can use a style like this.
Menu::create('zurb-top-bar', function($menu) {
$menu->style('zurb-top-bar');
});
If you don't like to use presenter classes, you use view presenters instead. We can set which view to present the menus by calling ->setView()
method.
Menu::create('navbar', function($menu) {
$menu->setView('menus::default');
});
View name | Menu style |
---|---|
menus::default | Bootstrap Navbar (default) |
menus::navbar-left | Bootstrap Navbar Left |
menus::navbar-right | Bootstrap Navbar Right |
menus::nav-tabs | Bootstrap Nav Tabs |
menus::nav-tabs-justified | Bootstrap Nav Tabs Justified |
menus::nav-pills | Bootstrap Nav Pills |
menus::nav-pills-stacked | Bootstrap Nav Pills Stacked |
menus::nav-pills-justified | Bootstrap Nav Pills Justified |
menus::menu | Plain Menu |