| CGI-Application-Plugin-Menu documentation | Contained in the CGI-Application-Plugin-Menu distribution. |
CGI::Application::Plugin::Menu - manage navigation menus for cgi apps
use base 'CGI::Application';
use CGI::Application::Plugin::Menu;
sub _get_menu_outputs {
my $self = shift;
my $html_output;
my $menu_main = $self->menu;
# resolves to a 'Home' link
$menu_main->add('/');
# makes into runmodelink for CGI::Application
# <a href='?rm=view_stats'>View Statistics</a>
$menu_main->add( view_stats, 'View Statistics'); # with label
# makes into runmodelink for CGI::Application
# <a href='?rm=view_history'>View History</a>
$menu_main->add('view_history'); # label is optional
my $menu_info = $self->menu('info');
$menu_info->add('/about_us.html');
$menu_info->add('/contact_info.html');
$html_output .=
$menu_main->output
. $menu_info->output;
return $html_output;
}
sub _inject_menus_into_tmpl {
my ($self,$tmpl) = @_;
my $m = $self->menu;
$m->add('home'); # ?rm=home / 'Home'
$m->add('account_view'); # ?rm=account_view / Account View
$m->add('account_edit','edit bogus'); # ?rm=account_edit / edit bogus
$m->add('account_delete');
$m->add('/info.html'); # /info.html / Info
$tmpl->param( MENU_LOOP => $m->loop);
}
sub _set_menus_in_template {
my $self = shift;
my $m = $self->menu('main');
$m->add('home','home page');
$m->add('view_stats');
$m->add('http://cpan.org','visit cpan');
$m->name; # returns 'main', for this example
# GETTING THE HTML TEMPLATE LOOP
my $main_menu_loop = $m->loop;
my $tmpl = $self->this_method_returns_HTML_Template_object;
$tmpl->param( 'MAIN_MENU' => $main_menu_loop );
#or
$tmpl->param( 'MAIN_MENU' => $self->menu_get('main_menu')->loop );
# IN YOUR HTML TEMPLATE:
#
# <ul>
# <TMPL_LOOP MAIN_MENU>
# <li><a href="<TMPL_VAR URL>"><TMPL_VAR LABEL></a></li>
# </TMPL_LOOP>
# </ul>
return 1;
}
This is a simple way of having menus in your cgi apps.
Are instances of HTML::Template::Menu
returns name of the menu.
argument is url or runmode optional argument is label
If the first argument has no funny chars, it is treated as a runmode.
The label is what will appear in the link text If not provided, one will be made. If you have a runmode called see_more, the link text is "See More".
The link will be
<a href="?=$YOURRUNMODEPARAMNAME=$ARG1">$ARG2</a>
So in this example:
$m->add('view_tasks');
The result is:
<a href="?rm=view_tasks">View Tasks</a>
get loop suitable for HTML::Template object See SYNOPSIS.
If you just want the output with the default hard coded template. The default template code is stored in: $HTML::Template::Menu::DEFAULT_TMPL
my $m = $self->menu_get('main menu');
$m->add('home');
$m->add('http://helpme.com','Need help?');
$m->add('logout');
Elements for the menu are shown in the order they are inserted.
See CGI::Application::Plugin::AutoMenuitem
Leo Charre leocharre at cpan dot org
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".
This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the "GNU General Public License" for more details.
| CGI-Application-Plugin-Menu documentation | Contained in the CGI-Application-Plugin-Menu distribution. |
package CGI::Application::Plugin::Menu; use strict; use LEOCHARRE::DEBUG; use warnings; use Carp; use Exporter; use HTML::Template::Menu; use vars qw(@ISA @EXPORT $VERSION); @ISA = qw/ Exporter /; @EXPORT = qw(menu ___menus_ ___menus_order menus menus_count menu_delete); $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)/g; sub menu { my ($self,$label) = @_; $label ||= 'main'; unless ( exists $self->___menus_->{$label} ) { $self->___menus_->{$label} = # new CGI::Application::Plugin::MenuObject; new HTML::Template::Menu; $self->___menus_->{$label}->name_set($label); push @{$self->___menus_order},$label; } return $self->___menus_->{$label}; } *menus = \&___menus_order; sub ___menus_ { $_[0]->{'__CGI::Application::Plugin::Menu::Objects'} ||={}; $_[0]->{'__CGI::Application::Plugin::Menu::Objects'}; } sub ___menus_order { $_[0]->{'__CGI::Application::Plugin::Menu::ObjectsOrder'} ||=[]; $_[0]->{'__CGI::Application::Plugin::Menu::ObjectsOrder'}; } sub menus_count { scalar @{$_[0]->menus} } sub menu_delete { my $self = shift; my $label = shift; delete $self->___menus_->{$label}; my @order; for my $menu_label ( @{$self->___menus_order} ){ $menu_label eq $label and next; push @order, $menu_label; } $self->{'__CGI::Application::Plugin::Menu::ObjectsOrder'} = \@order; 1; } 1; __END__