Revision 3cc77e53
Von Moritz Bunkus vor mehr als 14 Jahren hinzugefügt
SL/Template/Plugin/L.pm | ||
---|---|---|
10 | 10 |
return $::locale->quote_special_chars('HTML', $string); |
11 | 11 |
} |
12 | 12 |
|
13 |
sub _hashify { |
|
14 |
return (@_ && (ref($_[0]) eq 'HASH')) ? %{ $_[0] } : @_; |
|
15 |
} |
|
16 |
|
|
13 | 17 |
sub new { |
14 | 18 |
my $class = shift; |
15 | 19 |
my $context = shift; |
... | ... | |
29 | 33 |
|
30 | 34 |
sub attributes { |
31 | 35 |
my $self = shift; |
32 |
my $options = shift || {};
|
|
36 |
my %options = _hashify(@_);
|
|
33 | 37 |
|
34 | 38 |
my @result = (); |
35 |
while (my ($name, $value) = each %{ $options }) {
|
|
39 |
while (my ($name, $value) = each %options) {
|
|
36 | 40 |
next unless $name; |
37 | 41 |
$value ||= ''; |
38 | 42 |
push @result, _H($name) . '="' . _H($value) . '"'; |
... | ... | |
45 | 49 |
my $self = shift; |
46 | 50 |
my $tag = shift; |
47 | 51 |
my $content = shift; |
48 |
my $attributes = $self->attributes(shift || {});
|
|
52 |
my $attributes = $self->attributes(@_);
|
|
49 | 53 |
|
50 | 54 |
return "<${tag}${attributes}/>" unless $content; |
51 | 55 |
return "<${tag}${attributes}>${content}</${tag}>"; |
52 | 56 |
} |
53 | 57 |
|
54 | 58 |
sub select_tag { |
55 |
my $self = shift; |
|
56 |
my $name = shift; |
|
57 |
my $options_str = shift; |
|
58 |
my $attributes = shift || {}; |
|
59 |
my $self = shift; |
|
60 |
my $name = shift; |
|
61 |
my $options_str = shift; |
|
62 |
my %attributes = _hashify(@_); |
|
63 |
|
|
64 |
$attributes{id} ||= $self->name_to_id($name); |
|
65 |
|
|
66 |
return $self->html_tag('select', $options_str, %attributes, name => $name); |
|
67 |
} |
|
68 |
|
|
69 |
sub checkbox_tag { |
|
70 |
my $self = shift; |
|
71 |
my $name = shift; |
|
72 |
my %attributes = _hashify(@_); |
|
73 |
|
|
74 |
$attributes{id} ||= $self->name_to_id($name); |
|
75 |
$attributes{value} = 1 unless defined $attributes{value}; |
|
76 |
my $label = delete $attributes{label}; |
|
77 |
|
|
78 |
if ($attributes{checked}) { |
|
79 |
$attributes{checked} = 'checked'; |
|
80 |
} else { |
|
81 |
delete $attributes{checked}; |
|
82 |
} |
|
59 | 83 |
|
60 |
$attributes->{name} = $name;
|
|
61 |
$attributes->{id} ||= $self->name_to_id($name);
|
|
84 |
my $code = $self->html_tag('input', undef, %attributes, name => $name, type => 'checkbox');
|
|
85 |
$code .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
|
|
62 | 86 |
|
63 |
return $self->html_tag('select', $options_str, $attributes); |
|
87 |
return $code; |
|
88 |
} |
|
89 |
|
|
90 |
sub input_tag { |
|
91 |
my $self = shift; |
|
92 |
my $name = shift; |
|
93 |
my $value = shift; |
|
94 |
my %attributes = _hashify(@_); |
|
95 |
|
|
96 |
$attributes{id} ||= $self->name_to_id($name); |
|
97 |
$attributes{type} ||= 'text'; |
|
98 |
|
|
99 |
return $self->html_tag('input', undef, %attributes, name => $name, value => $value); |
|
64 | 100 |
} |
65 | 101 |
|
66 | 102 |
sub options_for_select { |
67 | 103 |
my $self = shift; |
68 | 104 |
my $collection = shift; |
69 |
my $options = shift || {};
|
|
105 |
my %options = _hashify(@_);
|
|
70 | 106 |
|
71 |
my $value_key = $options->{value} || 'id';
|
|
72 |
my $title_key = $options->{title} || $value_key;
|
|
107 |
my $value_key = $options{value} || 'id'; |
|
108 |
my $title_key = $options{title} || $value_key; |
|
73 | 109 |
|
74 | 110 |
my @tags = (); |
75 | 111 |
if ($collection && (ref $collection eq 'ARRAY')) { |
... | ... | |
80 | 116 |
: ( $element->$value_key, $element->$title_key ); |
81 | 117 |
|
82 | 118 |
my %attributes = ( value => $result[0] ); |
83 |
$attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || ''));
|
|
119 |
$attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result[0] || ''));
|
|
84 | 120 |
|
85 |
push @tags, $self->html_tag('option', _H($result[1]), \%attributes);
|
|
121 |
push @tags, $self->html_tag('option', _H($result[1]), %attributes); |
|
86 | 122 |
} |
87 | 123 |
} |
88 | 124 |
|
... | ... | |
124 | 160 |
|
125 | 161 |
Converts a name to a HTML id by replacing various characters. |
126 | 162 |
|
127 |
=item C<attributes \%items>
|
|
163 |
=item C<attributes %items> |
|
128 | 164 |
|
129 |
Creates a string from all elements in C<\%items> suitable for usage as
|
|
165 |
Creates a string from all elements in C<%items> suitable for usage as |
|
130 | 166 |
HTML tag attributes. Keys and values are HTML escaped even though keys |
131 | 167 |
must not contain non-ASCII characters for browsers to accept them. |
132 | 168 |
|
133 |
=item C<html_tag $tag_name, $content_string, \%attributes>
|
|
169 |
=item C<html_tag $tag_name, $content_string, %attributes> |
|
134 | 170 |
|
135 | 171 |
Creates an opening and closing HTML tag for C<$tag_name> and puts |
136 | 172 |
C<$content_string> between the two. If C<$content_string> is undefined |
... | ... | |
145 | 181 |
|
146 | 182 |
=over 4 |
147 | 183 |
|
148 |
=item C<select_tag $name, $options_string, \%attributes>
|
|
184 |
=item C<select_tag $name, $options_string, %attributes> |
|
149 | 185 |
|
150 |
Creates a HTML 'select' tag named $name with the contents
|
|
151 |
$options_string and with arbitrary HTML attributes from
|
|
152 |
C<\%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
|
|
186 |
Creates a HTML 'select' tag named C<$name> with the contents
|
|
187 |
C<$options_string> and with arbitrary HTML attributes from
|
|
188 |
C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>. |
|
153 | 189 |
|
154 | 190 |
The $options_string is usually created by the C<options_for_select> |
155 | 191 |
function. |
156 | 192 |
|
193 |
=item C<input_tag $name, $value, %attributes> |
|
194 |
|
|
195 |
Creates a HTML 'input type=text' tag named C<$name> with the value |
|
196 |
C<$value> and with arbitrary HTML attributes from C<%attributes>. The |
|
197 |
tag's C<id> defaults to C<name_to_id($name)>. |
|
198 |
|
|
199 |
=item C<checkbox_tag $name, %attributes> |
|
200 |
|
|
201 |
Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary |
|
202 |
HTML attributes from C<%attributes>. The tag's C<id> defaults to |
|
203 |
C<name_to_id($name)>. The tag's C<value> defaults to C<1>. |
|
204 |
|
|
205 |
If C<%attributes> contains a key C<label> then a HTML 'label' tag is |
|
206 |
created with said C<label>. No attribute named C<label> is created in |
|
207 |
that case. |
|
208 |
|
|
157 | 209 |
=back |
158 | 210 |
|
159 | 211 |
=head2 CONVERSION FUNCTIONS |
160 | 212 |
|
161 | 213 |
=over 4 |
162 | 214 |
|
163 |
=item C<options_for_select \@collection, \%options>
|
|
215 |
=item C<options_for_select \@collection, %options> |
|
164 | 216 |
|
165 | 217 |
Creates a string suitable for a HTML 'select' tag consisting of one |
166 | 218 |
'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value |
... | ... | |
174 | 226 |
|
175 | 227 |
=item 2. A scalar. The scalar is both the value and the title. |
176 | 228 |
|
177 |
=item 3. A hash reference. In this case C<\%options> must contain
|
|
229 |
=item 3. A hash reference. In this case C<%options> must contain |
|
178 | 230 |
I<value> and I<title> keys that name the keys in the element to use |
179 | 231 |
for the value and title respectively. |
180 | 232 |
|
181 |
=item 4. A blessed reference. In this case C<\%options> must contain
|
|
233 |
=item 4. A blessed reference. In this case C<%options> must contain |
|
182 | 234 |
I<value> and I<title> keys that name functions called on the blessed |
183 | 235 |
reference whose return values are used as the value and title |
184 | 236 |
respectively. |
Auch abrufbar als: Unified diff
Hilfsfunktionen zum Erzeugen von Checkbox- und Text-Input-Tags