HTML::Widgets::NavMenu(3pm) | User Contributed Perl Documentation | HTML::Widgets::NavMenu(3pm) |
HTML::Widgets::NavMenu - A Perl Module for Generating HTML Navigation Menus
version 1.1000
use HTML::Widgets::NavMenu; my $nav_menu = HTML::Widgets::NavMenu->new( 'path_info' => "/me/", 'current_host' => "default", 'hosts' => { 'default' => { 'base_url' => "http://www.hello.com/" }, }, 'tree_contents' => { 'host' => "default", 'text' => "Top 1", 'title' => "T1 Title", 'expand_re' => "", 'subs' => [ { 'text' => "Home", 'url' => "", }, { 'text' => "About Me", 'title' => "About Myself", 'url' => "me/", }, ], }, ); my $results = $nav_menu->render(); my $nav_menu_html = join("\n", @{$results->{'html'}});
This module generates a navigation menu for a site. It can also generate a complete site map, a path of leading components, and also keeps track of navigation links ("Next", "Prev", "Up", etc.) You can start from the example above and see more examples in the tests, in the "examples/" directory of the HTML-Widgets-NavMenu tarball, and complete working sites in the version control repositories at <https://www.shlomifish.org/meta/site-source/> and <https://perl-begin.org/source/> .
To use this module call the constructor with the following named arguments:
Currently the only key required in the hash is the "base_url" one that points to a string containing the absolute URL to the sub-site. The base URL may have trailing components if it does not reside on the domain's root directory.
An optional key that is required only if you wish to use the "site_abs" url_type (see below), is "trailing_url_base", which denotes the component of the site that appears after the hostname. For "http://www.myhost.com/~myuser/" it is "/~myuser/".
Here's an example for a minimal hosts value:
'hosts' => { 'default' => { 'base_url' => "http://www.hello.com/", 'trailing_url_base' => "/", }, },
And here's a two-hosts value from my personal site, which is spread across two sites:
'hosts' => { 't2' => { 'base_url' => "http://www.shlomifish.org/", 'trailing_url_base' => "/", }, 'vipe' => { 'base_url' => "http://vipe.technion.ac.il/~shlomif/", 'trailing_url_base' => "/~shlomif/", }, },
For example, assigning:
'ul_classes' => [ "FirstClass", "second myclass", "3C" ],
Will assign "FirstClass" as the class of the top-most ULs, "second myclass" as the classes of the ULs inner to it, and "3C" as the class of the ULs inner to the latter ULs.
If classes are undef, the UL tag will not contain a class parameter.
A complete invocation of an HTML::Widgets::NavMenu constructor can be found in the SYNOPSIS above.
After you _init an instance of the navigation menu object, you need to get the results using the render function.
render() should be called after a navigation menu object is constructed to prepare the results and return them. It returns a hash reference with the following keys:
my $nav_links = $results->{'nav_links_obj'}; # Sort the keys so their order will be preserved my @keys = (sort { $a cmp $b } keys(%$nav_links)); foreach my $key (@keys) { my $value = $nav_links->{$key}; my $url = CGI::escapeHTML($value->direct_url()); my $title = CGI::escapeHTML($value->title()); print {$fh} "<link rel=\"$key\" href=\"$url\" title=\"$title\" />\n"; }
This sample code renders the links as "<link rel=...>" into the page header:
my $nav_links = $results->{'nav_links'}; # Sort the keys so their order will be preserved my @keys = (sort { $a cmp $b } keys(%$nav_links)); foreach my $key (@keys) { my $url = $nav_links->{$key}; print {$fh} "<link rel=\"$key\" href=\"" . CGI::escapeHTML($url) . "\" />\n"; }
Renders a fully expanded tree suitable for input to JQuery's treeview plugin: <http://bassistance.de/jquery-plugins/jquery-plugin-treeview/> - otherwise the same as render() .
This function can be called to generate a site map based on the tree of contents. It returns a reference to an array containing the tags of the site map.
This function can be called to calculate a URL to a different part of the site. It accepts four named arguments, passed as a hash-ref:
This is like get_cross_host_rel_url_ref() except that the arguments are clobbered into the arguments list. It is kept here for compatibility sake.
The input tree is a nested Perl data structure that represnets the tree of the site. Each node is respresented as a Perl hash reference, with its sub-nodes contained in an array reference of its 'subs' value. A non-existent 'subs' means that the node is a leaf and has no sub-nodes.
The top-most node is mostly a dummy node, that just serves as the father of all other nodes.
Following is a listing of the possible values inside a node hash and what their respective values mean.
Generally, a host must always be specified and so the first node should specify it.
The URL should be specified for every nodes except separators and the such.
<a href="my-url/">Hi There</a>
Or
<b>Hi There</b>
if it's the current page. Not that this text is rendered into HTML as is, and so should be escaped to prevent HTML-injection attacks.
If 'separator' is specified, it is usually meaningless to specify all other node keys except 'skip'.
1. "rel" - the default. This means a fully relative URL (if possible), like "../../me/about.html".
2. "site_abs" - this uses a URL absolute to the site, using a slash at the beginning. Like "/~shlomif/me/about.html". For this to work the current host needs to have a 'trailing_url_base' value set.
3. "full_abs" - this uses a fully qualified URL (e.g: with "http://" at the beginning, even if both the current path and the pointed path belong to the same host. Something like "http://www.shlomifish.org/me/about.html".
Note that using absolute URLs as part of the site flow is discouraged because once they are accessed, the navigation within the primary site is lost. A better idea would be to create a separate page within the site, that will link to the external URL.
'tree_contents' => { 'host' => "default", 'text' => "Top 1", 'title' => "T1 Title", 'expand_re' => "", 'subs' => [ { 'text' => "Home", 'url' => "", }, { 'text' => "About Me", 'title' => "About Myself", 'url' => "me/", 'li_id' => 'about_me', }, ], },
Then the HTML for the About me will look something like:
<li id="about_me"> <a href="me/ title="About Myself">About Me</a> </li>
An explicitly specified predicate value is a hash reference that contains one of the following three keys with their appropriate values:
Here is an example for such a callback:
sub predicate_cb1 { my %args = (@_); my $host = $args{'current_host'}; my $path = $args{'path_info'}; return (($host eq "true") && ($path eq "mypath/")); }
Note that if 'cb' is specified then both 're' and 'bool' will be ignored, and 're' over-rides 'bool'.
Orthogonal to these keys is the 'capt' key which specifies whether this expansion "captures" or not. This is relevant to the behaviour in the breadcrumbs' trails, if one wants the item to appear there or not. The default value is true.
If the predicate is not a hash reference, then HTML::Widgets::NavMenu will try to guess what it is. If it's a sub-routine reference, it will be an implicit callback. If it's one of the values "0", "1", "yes", "no", "true", "false", "True", "False" it will be considered a boolean. If it's a different string, a regular expression match will be attempted. Else, an excpetion will be thrown.
Here are some examples for predicates:
# Always expand. 'expand' => { 'bool' => 1, }; # Never expand. 'expand' => { 'bool' => 0, }; # Expand under home/ 'expand' => { 're' => "^home/" }, # Expand under home/ when the current host is "foo" sub expand_path_home_host_foo { my %args = (@_); my $host = $args{'current_host'}; my $path = $args{'path_info'}; return (($host eq "foo") && ($path =~ m!^home/!)); } 'expand' => { 'cb' => \&expand_path_home_host_foo, },
When retrieving the leading path or the "nav_links_obj", an array of objects is returned. This section describes the class of these objects, so one will know how to use them.
Basically, it is an object that has several accessors. The accessors are:
See the article Shlomi Fish wrote for Perl.com for a gentle introduction to HTML-Widgets-NavMenu:
<http://www.perl.com/pub/a/2005/07/07/navwidgets.html>
Shlomi Fish, <shlomif@cpan.org>, <http://www.shlomifish.org/> .
Thanks to Yosef Meller (<https://metacpan.org/author/YOSEFM>) for writing the module HTML::Widget::SideBar on which initial versions of these modules were based. (Albeit his code is no longer used here.)
Copyright 2004, Shlomi Fish. All rights reserved.
You can use, modify and distribute this module under the terms of the MIT Expat license. ( <http://www.opensource.org/licenses/mit-license.php> ).
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.
A modern, open-source CPAN search engine, useful to view POD in HTML format.
<https://metacpan.org/release/HTML-Widgets-NavMenu>
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
<https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-Widgets-NavMenu>
The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
<http://cpants.cpanauthors.org/dist/HTML-Widgets-NavMenu>
The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
<http://www.cpantesters.org/distro/H/HTML-Widgets-NavMenu>
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
<http://matrix.cpantesters.org/?dist=HTML-Widgets-NavMenu>
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
<http://deps.cpantesters.org/?module=HTML::Widgets::NavMenu>
Please report any bugs or feature requests by email to "bug-html-widgets-navmenu at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-Widgets-NavMenu>. You will be automatically notified of any progress on the request by the system.
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/shlomif/perl-HTML-Widgets-NavMenu>
git clone git://github.com/shlomif/perl-HTML-Widgets-NavMenu.git
Shlomi Fish <shlomif@cpan.org>
Please report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/perl-HTML-Widgets-NavMenu/issues>
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
This software is Copyright (c) 2005 by Shlomi Fish.
This is free software, licensed under:
The MIT (X11) License
2022-08-20 | perl v5.34.0 |