| HTML-Widgets-NavMenu documentation | view source | Contained in the HTML-Widgets-NavMenu distribution. |
HTML::Widgets::NavMenu - A Perl Module for Generating HTML Navigation Menus
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, and complete working sites in the Subversion repositories at http://opensvn.csie.org/shlomif/homepage/ and http://opensvn.csie.org/perlbegin/perl-begin/.
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.
This is the host-ID of the host as found in the 'hosts' key to the
navigation menu object constructor. It implicitly propagates downwards in the
tree. (i.e: all nodes of the sub-tree spanning from the node will implicitly
have it as their value by default.)
Generally, a host must always be specified and so the first node should specify it.
This contains the URL of the node within the host. The URL should not contain a leading slash. This value does not propagate further.
The URL should be specified for every nodes except separators and the such.
This is the text that will be presented to the user as the text of the
link inside the navigation bar. E.g.: if 'text' is "Hi There", then the
link will look something like this:
<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.
This is the text of the link tag's title attribute. It is also not processed and so the user of the module should make sure it is escaped if needed, to prevent HTML-injection attacks. It is optional, and if not specified, no title will be presented.
This item, if specified, should point to an array reference containing the sub-nodes of this item, in order.
This key if specified and true indicate that the item is a separator, which
should just leave a blank line in the HTML. It is best to accompany it with
'skip' (see below).
If 'separator' is specified, it is usually meaningless to specify all
other node keys except 'skip'.
This key if true, indicates that the node should be skipped when traversing site using the Mozilla navigation links. Instead the navigation will move to the next or previous nodes.
This key if true indicates that the item should be part of the site's flow and site map, but not displayed in the navigation menu.
This indicates a role of an item. It is similar to a CSS class, or to DocBook's "role" attribute, only induces different HTML markup. The vanilla HTML::Widgets::NavMenu does not distinguish between any roles, but see HTML::Widgets::NavMenu::HeaderRole.
This specifies a predicate (a Perl value that is evaluated to a boolean value, see "Predicate Values" below.) to be matched against the path and current host to determine if the navigation menu should be expanded at this node. If it does, all of the nodes up to it will expand as well.
This value if true, indicates that the node and all nodes below it (until
'show_always' is explicitly set to false) must be always displayed. Its
function is similar to 'expand_re' but its propagation semantics the
opposite.
This specifies the URL type to use to render this item. It can be:
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.
This is similar to 'url_type' only it recurses, to the sub-tree of the
node. If both 'url_type' and 'rec_url_type' are specified for a node,
then the value of 'url_type' will hold.
This flag, if true, indicates that the URL specified by the 'url' key
is an absolute URL like http://www.myhost.com/ and should not be
treated as a path within the site. All links to the page associated with
this node will contain the URL verbatim.
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.
An explicitly specified predicate value is a hash reference that contains one of the following three keys with their appropriate values:
This specifies a sub-routine reference (or "callback" or "cb"), that will be
called to determine the result of the predicate. It accepts two named arguments
- 'path_info' which is the path of the current page (without the leading
slash) and 'current_host' which is the ID of the current host.
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/"));
}
This specifies a regular expression to be matched against the path_info (regardless of what current_host is), to determine the result of the predicate.
This specifies the constant boolean value of the predicate.
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:
The host ID of this node.
The URL of the node within the host. (one given in its 'url' key).
The label of the node. (one given in its 'text' key). This is not SGML-escaped.
The title of the node. (that can be assigned to the URL 'title' attribute). This is not SGML-escaped.
A direct URL (usable for inclusion in an A tag ) from the current page to this page.
This is the url_type (see above) that holds for this node.
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
An HTML::Widgets::NavMenu sub-class that contains support for another role. Used for the navigation menu in http://perl-begin.berlios.de/.
A module written by Yosef Meller for maintaining a navigation menu. HTML::Widgets::NavMenu originally utilized it, but no longer does. This module does not makes links relative on its own, and tends to generate a lot of JavaScript code by default. It also does not have too many automated test scripts.
A module by Don Owens for generating hierarchical HTML menus. I could not quite understand its tree traversal semantics, so I ended up not using it. Also seems to require that each of the tree node will have a unique ID.
This module also generates a navigation menu. The CPAN version is relatively old, and the author sent me a newer version. After playing with it a bit, I realized that I could not get it to do what I want (but I cannot recall why), so I abandoned it.
Shlomi Fish <shlomif@iglu.org.il> (http://search.cpan.org/~shlomif/).
Thanks to Yosef Meller (http://search.cpan.org/~yosefm/) for writing the module HTML::Widget::SideBar on which initial versions of this 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 X11 license. ( http://www.opensource.org/licenses/mit-license.php ).
| HTML-Widgets-NavMenu documentation | view source | Contained in the HTML-Widgets-NavMenu distribution. |