symplely / menu
A simple menu builder in PHP
1.0.1
2019-11-26 17:25 UTC
Requires
- php: >5.6
Requires (Dev)
- phpunit/phpunit: >5.7.0
This package is auto-updated.
Last update: 2024-11-04 16:38:29 UTC
README
A simple menu builder in PHP
Installation
To install this library make sure you have composer installed, then run following command:
composer require symplely/menu
Usage
A menu can be instantiated and items can be added fluently with the add
method,
which requires a url and a string of text as parameters.
<?php require_once 'vendor/autoload.php'; $menu = new Async\Menu\Menu; // The main menu $menu->add('Home', ''); // Creates a sub menu $about = $menu->add('About', 'about'); // Creates a another sub menu from the sub menu, passing url and other attributes in key = value pair. $support = $about->add('Who we are?', ['url' => 'who-we-are', 'class' => 'navbar-item who']); // Add items to sub menu, passing url and other attributes in key = value pair. $about->add('What we do?', ['url' => 'what-we-do', 'class' => 'navbar-item what']); // Item has sub items we append a caret icon to the hyperlink text $about->append(' <span class="caret"></span>'); // Or just the preset, $default = 'caret' $about->caret($default); // Attach HTML attributes to the hyper-link as a key = value array $about->attributes(['class' => 'link-item', 'target' => '_blank']); // Or Separately $about->attributes('data-model', 'nice'); // Or the shorter $about->addClass('link-item'); $about->addTarget('_blank'); // Add more items to the main menu $menu->add('Portfolio', 'portfolio'); $menu->add('Contact', 'contact'); // we're only going to hide items with `display` set to **false** $menu->filter( function($item) { if ($item->meta('display') === false) { return false; } return true; }); // Now we can render the menu as various HTML entities: echo $menu->renderUnordered(['class' => 'some-ul']); // OR echo $menu->renderOrdered(['class' => 'some-ol']); // OR echo $menu->renderDiv(['class' => 'some-div']); // For bootstrap users echo bootstrap_menu($menu);
Let's get things started by building a simple menu with two links. All of the following examples are using classes from the Async\Menu
namespace.
<ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> </ul>
$menu = new Link; $menu->add('Home', '/') $menu->add('About', '/about'));
When we render or echo the menu, it will output our intended html string.
// Via the `render` method: echo $menu->render(); // Or just through `__toString`: echo $menu;
<ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> </ul>
The functional approach
$menuInstance = \create_menu($url, $urlName, $iconImage, $iconType, $anyPreviousMenuInstance); $subMenu = \create_menuSub($menuInstance, $url, $urlName, $iconImage, $iconType); \create_menuItem($subMenu, $url, $urlName, $iconImage, $iconType); print $menuInstance->render()); // Or print \bootstrap_menu($menuInstance);