Dancer2::Plugin::Menu - Automatically generate an HTML menu for your Dancer2 app
version 0.009
In your app:
use Dancer2; use Dancer2::Plugin::Menu; menu_item( { title => 'My Parent Item', weight => 3 }, get 'path' => sub { template }, ); menu_item( { title => 'My Child1 Item', weight => 3 }, get 'path/menu1' => sub { template }, ); menu_item( { title => 'My Child2 Item', weight => 4 }, get 'path/menu2' => sub { template }, );
In your template file:
<% menu %>
This will generate a hierarchical menu that will look like this when the path/menu1 route is visited:
path/menu1
<ul><li class="active">Path <ul><li class="active">My Child1 Item</li> <li>My Child2 Item</li> </ul> </ul>
This module generates HTML for routes wrapped in the menu_item keyword. Menu items will be injected into the template wherever the <% menu %> tag is located. Child menu items are wrapped in <li> HTML tags which are themselves wrapped in a <ul> tag associated with the parent menu item. Menu items within the current route are given the active class so they can be styled.
menu_item
<li>
<ul>
active
The module is in early development stages and currently has few options. It has not been heavily tested and there are likely bugs especially with dynamic paths which are completely untested at this time. The module should work and be adequate for simple menu structures, however.
ROUTE METHOD
REGEXP
CODE
Wraps a conventional route handler preceded by a required hash reference containing data that will be applied to the route's endpoint.
Two keys can be supplied in the hash reference: a title for the menu item and a weight. The title will be used as the content for the menu items. The weight will determine the order of the menu items. Heavier items (with larger values) will "sink" to the bottom compared to sibling menu items. If two sibling menu items have the same weight, the menu items will be ordered alphabetically.
title
weight
Menu items that are not endpoints in the route or that don't have a title, will automatically generate a title according to the path segment's name. For example, the route /categories/fun food/desserts is converted to a hierarchy of menu items entitled Categories, Fun food, and Desserts. Note that capitalization is automatically added. Automatic titles will be overridden with endpoint specific titles if they are supplied in a later menu_item call.
/categories/fun food/desserts
Categories
Fun food
Desserts
If the weight is not supplied it will default to a value of 5.
5
Add a <% menu %> tag in the appropriate location within your Dancer2 template files. If desired, add css for <li> tags in the active class.
Dancer2::Core::Hook
Dancer2::Plugin
Data::Dumper
HTML::Element
MooX::HandlesVia
Storable
strict
warnings
You can find documentation for this module with the perldoc command.
perldoc Dancer2::Plugin::Menu
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
MetaCPAN
A modern, open-source CPAN search engine, useful to view POD in HTML format.
https://metacpan.org/release/Dancer2-Plugin-Menu
The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)
https://github.com/sdondley/Dancer2-Plugin-Menu
git clone git://github.com/sdondley/Dancer2-Plugin-Menu.git
You can make new bug reports, and view existing ones, through the web interface at https://github.com/sdondley/Dancer2-Plugin-Menu/issues.
See perlmodinstall for information and options on installing Perl modules.
Dancer2
Steve Dondley <s@dondley.com>
This software is copyright (c) 2018 by Steve Dondley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer2::Plugin::Menu, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::Menu
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::Menu
For more information on module installation, please visit the detailed CPAN module installation guide.