Thread Dynamische Navigation mit HTML::Template (22 answers)
Opened by tonewheel at 2007-05-01 13:11

Ronnie
 2007-05-01 14:25
#28967 #28967
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Ich empfehle auch CPAN:HTML::Template::Compiled. Ansonsten könnte ich mir einen entsprechenden Ansatz ungefähr so vorstellen:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

use strict;
use warnings;

package HTML::Widget::NavEntry;
use Moose;

has 'title' => ( is => 'rw' );
has 'link' => ( is => 'rw' );

sub to_html {
my $self = shift;
return "\t" . '<a href="' . $self->link . '">' . $self->title . '</a>' . "\n";
}

package HTML::Widget::SubNavigation;
use Moose;

has 'title' => ( is => 'rw', required => 1 );
has 'entries' => (
isa => 'ArrayRef',
is => 'rw',
default => sub { return [] }
);

sub to_html {
my $self = shift;
my $o = '<div id="nav_' . $self->title . '">' . "\n";
$o .= $_->to_html for @{$self->entries};
$o .= '</div>' . "\n";
return $o;
}

sub add_entry {
my $self = shift;
my $entry = shift;
return unless blessed $entry eq 'HTML::Widget::NavEntry';
push @{$self->entries}, $entry;
}

package HTML::Widget::Navigation;
use Moose;

has 'entries' => (
isa => 'ArrayRef',
is => 'rw',
default => sub { return [] }
);

sub to_html {
my $self = shift;
my $o = '<div id="navigation">' . "\n";
$o .= $_->to_html for @{$self->entries};
$o .= '</div>' . "\n" x 2;
return $o;
}

sub selected_to_html {
my $self = shift;
my $selection = shift;

my $o = '<div id="navigation">' . "\n";
$o .= $_->to_html for (grep { $_->title eq $selection } @{$self->entries});
$o .= '</div>' . "\n" x 2;
return $o;
}

sub add_entry {
my $self = shift;
my $entry = shift;
return unless (
blessed $entry eq 'HTML::Widget::NavEntry'
or blessed $entry eq 'HTML::Widget::SubNavigation'
);
push @{$self->entries}, $entry;
}

package main;

my $s_nav1 = HTML::Widget::SubNavigation->new( title => 'foo' );

$s_nav1->add_entry(
HTML::Widget::NavEntry->new( title => 'foo', link => 'foo.pl' )
);

$s_nav1->add_entry(
HTML::Widget::NavEntry->new(
title => 'bar',
link => 'foo.pl?content=bar'
)
);

$s_nav1->add_entry(
HTML::Widget::NavEntry->new( title => 'buz', link => 'buz.pl' )
);

my $s_nav2 = HTML::Widget::SubNavigation->new( title => 'yankee' );

$s_nav2->add_entry(
HTML::Widget::NavEntry->new( title => 'yankee', link => 'yankee.pl' )
);

$s_nav2->add_entry(
HTML::Widget::NavEntry->new( title => 'zulu', link => 'zulu.pl' )
);

my $nav = HTML::Widget::Navigation->new;
$nav->add_entry( $s_nav1 );
$nav->add_entry( $s_nav2 );

print $nav->to_html;
print $nav->selected_to_html('foo');

Du müsstest nur einen Weg finden dem Formular jedesmal die Info mitzugeben welche SubNavigation du angezeigt haben willst.

Die Ausgabe sieht beispielhaft so aus:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
<div id="navigation">
<div id="nav_foo">
<a href="foo.pl">foo</a>
<a href="foo.pl?content=bar">bar</a>
<a href="buz.pl">buz</a>
</div>
<div id="nav_yankee">
<a href="yankee.pl">yankee</a>
<a href="zulu.pl">zulu</a>
</div>
</div>

bzw. mit Auswahl auf 'foo':
Code: (dl )
1
2
3
4
5
6
7
<div id="navigation">
<div id="nav_foo">
<a href="foo.pl">foo</a>
<a href="foo.pl?content=bar">bar</a>
<a href="buz.pl">buz</a>
</div>
</div>

EDIT: Evtl. könnte man sowas nett mit CPAN:CGI::Ajax kombinieren?!\n\n

<!--EDIT|Ronnie|1178015217-->

View full thread Dynamische Navigation mit HTML::Template