symplely / menu
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-09-04 16:18:57 UTC
README
PHP 简单菜单构建器
安装
要安装此库,请确保您已安装 composer,然后运行以下命令
composer require symplely/menu
用法
可以使用 menu 类实例化并使用 add
方法流畅地添加项目,该方法需要 url 和 文本 字符串作为参数。
<?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);
让我们从构建一个包含两个链接的简单菜单开始。以下所有示例都使用了来自 Async\Menu
命名空间的类。
<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'));
当我们渲染或输出菜单时,它将输出我们预期的 HTML 字符串。
// 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>
功能方法
$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);