Revision 8177c150
Von Moritz Bunkus vor mehr als 14 Jahren hinzugefügt
SL/Template/Plugin/L.pm | ||
---|---|---|
1 |
package SL::Template::Plugin::L; |
|
2 |
|
|
3 |
use base qw( Template::Plugin ); |
|
4 |
use Template::Plugin; |
|
5 |
|
|
6 |
use strict; |
|
7 |
|
|
8 |
sub new { |
|
9 |
my $class = shift; |
|
10 |
my $context = shift; |
|
11 |
|
|
12 |
return bless { }, $class; |
|
13 |
} |
|
14 |
|
|
15 |
sub attributes { |
|
16 |
my $self = shift; |
|
17 |
my $options = shift || {}; |
|
18 |
|
|
19 |
my @result = (); |
|
20 |
while (my ($name, $value) = each %{ $options }) { |
|
21 |
next unless $name; |
|
22 |
$value ||= ''; |
|
23 |
push @result, "${name}=\"" . $::locale->quote_special_chars('HTML', $value) . '"'; |
|
24 |
} |
|
25 |
|
|
26 |
return @result ? ' ' . join(' ', @result) : ''; |
|
27 |
} |
|
28 |
|
|
29 |
sub html_tag { |
|
30 |
my $self = shift; |
|
31 |
my $tag = shift; |
|
32 |
my $content = shift; |
|
33 |
my $attributes = $self->attributes(shift || {}); |
|
34 |
|
|
35 |
return "<${tag}${attributes}/>" unless $content; |
|
36 |
return "<${tag}${attributes}>${content}</${tag}>"; |
|
37 |
} |
|
38 |
|
|
39 |
sub select_tag { |
|
40 |
my $self = shift; |
|
41 |
my $name = shift; |
|
42 |
my $options_str = shift; |
|
43 |
my $attributes = shift || {}; |
|
44 |
|
|
45 |
$attributes->{name} = $name; |
|
46 |
$attributes->{id} ||= $name; |
|
47 |
|
|
48 |
return $self->html_tag('select', $options_str, $attributes); |
|
49 |
} |
|
50 |
|
|
51 |
sub options_for_select { |
|
52 |
my $self = shift; |
|
53 |
my $collection = shift; |
|
54 |
my $options = shift || {}; |
|
55 |
|
|
56 |
my $value_key = $options->{value} || 'id'; |
|
57 |
my $title_key = $options->{title} || $value_key; |
|
58 |
|
|
59 |
my @tags = (); |
|
60 |
if ($collection && (ref $collection eq 'ARRAY')) { |
|
61 |
foreach my $element (@{ $collection }) { |
|
62 |
my @result = !ref $element ? ( $element, $element ) |
|
63 |
: ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] ) |
|
64 |
: ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} ) |
|
65 |
: ( $element->$value_key, $element->$title_key ); |
|
66 |
|
|
67 |
my %attributes = ( value => $result[0] ); |
|
68 |
$attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || '')); |
|
69 |
|
|
70 |
push @tags, $self->html_tag('option', $result[1], \%attributes); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
return join('', @tags); |
|
75 |
} |
|
76 |
|
|
77 |
1; |
|
78 |
|
|
79 |
__END__ |
|
80 |
|
|
81 |
=head1 NAME |
|
82 |
|
|
83 |
SL::Templates::Plugin::L -- Layouting / tag generation |
|
84 |
|
|
85 |
=head1 SYNOPSIS |
|
86 |
|
|
87 |
Usage from a template: |
|
88 |
|
|
89 |
[% USE L %] |
|
90 |
|
|
91 |
[% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %] |
|
92 |
|
|
93 |
[% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' }, |
|
94 |
{ direction => 'right', display => 'To the right' } ], |
|
95 |
value => 'direction', title => 'display', default => 'right')) %] |
|
96 |
|
|
97 |
=head1 DESCRIPTION |
|
98 |
|
|
99 |
A module modeled a bit after Rails' ActionView helpers. Several small |
|
100 |
functions that create HTML tags from various kinds of data sources. |
|
101 |
|
|
102 |
=head1 FUNCTIONS |
|
103 |
|
|
104 |
=over 4 |
|
105 |
|
|
106 |
=item C<attributes \%items> |
|
107 |
|
|
108 |
Creates a string from all elements in C<\%items> suitable for usage as |
|
109 |
HTML tag attributes. Keys and values are HTML escaped even though keys |
|
110 |
must not contain non-ASCII characters for browsers to accept them. |
|
111 |
|
|
112 |
=item C<html_tag $tag_name, $content_string, \%attributes> |
|
113 |
|
|
114 |
Creates an opening and closing HTML tag for C<$tag_name> and puts |
|
115 |
C<$content_string> between the two. If C<$content_string> is undefined |
|
116 |
or empty then only a E<lt>tag/E<gt> tag will be created. Attributes |
|
117 |
are key/value pairs added to the opening tag. |
|
118 |
|
|
119 |
=item C<options_for_select \@collection, \%options> |
|
120 |
|
|
121 |
Creates a string suitable for a HTML 'select' tag consisting of one |
|
122 |
'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value |
|
123 |
to use and the title to display are extracted from the elements in |
|
124 |
C<\@collection>. Each element can be one of four things: |
|
125 |
|
|
126 |
=over 12 |
|
127 |
|
|
128 |
=item 1. An array reference with at least two elements. The first element is |
|
129 |
the value, the second element is its title. |
|
130 |
|
|
131 |
=item 2. A scalar. The scalar is both the value and the title. |
|
132 |
|
|
133 |
=item 3. A hash reference. In this case C<\%options> must contain |
|
134 |
I<value> and I<title> keys that name the keys in the element to use |
|
135 |
for the value and title respectively. |
|
136 |
|
|
137 |
=item 4. A blessed reference. In this case C<\%options> must contain |
|
138 |
I<value> and I<title> keys that name functions called on the blessed |
|
139 |
reference whose return values are used as the value and title |
|
140 |
respectively. |
|
141 |
|
|
142 |
=back |
|
143 |
|
|
144 |
For cases 3 and 4 C<$options{value}> defaults to C<id> and |
|
145 |
C<$options{title}> defaults to C<$options{value}>. |
|
146 |
|
|
147 |
=item C<select_tag $name, $options_string, \%attributes> |
|
148 |
|
|
149 |
Creates a HTML 'select' tag named $name with the contents |
|
150 |
$options_string and with arbitrary HTML attributes from |
|
151 |
C<\%attributes>. The tag's C<id> defaults to C<$name>. |
|
152 |
|
|
153 |
The $options_string is usually created by the C<options_for_select> |
|
154 |
function. |
|
155 |
|
|
156 |
=back |
|
157 |
|
|
158 |
=head1 MODULE AUTHORS |
|
159 |
|
|
160 |
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt> |
|
161 |
|
|
162 |
L<http://linet-services.de> |
Auch abrufbar als: Unified diff
Ein Plugin zum Erzeugen von HTML-Tags auf die Rails-Art.