Revision 93bae7aa
Von Sven Schöling vor fast 14 Jahren hinzugefügt
SL/Template/Plugin/L.pm | ||
---|---|---|
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
sub options_for_select { |
155 |
my $self = shift; |
|
156 |
my $collection = shift; |
|
157 |
my %options = _hashify(@_); |
|
155 |
my $self = shift;
|
|
156 |
my $collection = shift;
|
|
157 |
my %options = _hashify(@_);
|
|
158 | 158 |
|
159 |
my $value_key = $options{value} || 'id'; |
|
160 |
my $title_key = $options{title} || $value_key; |
|
159 |
my $value_key = $options{value} || 'id';
|
|
160 |
my $title_key = $options{title} || $value_key;
|
|
161 | 161 |
|
162 |
my @elements = ();
|
|
163 |
push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
|
|
162 |
my $value_sub = $options{value_sub};
|
|
163 |
my $title_sub = $options{title_sub};
|
|
164 | 164 |
|
165 |
if ($collection && (ref $collection eq 'ARRAY')) { |
|
166 |
foreach my $element (@{ $collection }) { |
|
167 |
my @result = !ref $element ? ( $element, $element ) |
|
168 |
: ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] ) |
|
169 |
: ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} ) |
|
170 |
: ( $element->$value_key, $element->$title_key ); |
|
165 |
my $value_title_sub = $options{value_title_sub}; |
|
171 | 166 |
|
172 |
push @elements, \@result; |
|
173 |
} |
|
174 |
} |
|
167 |
my $access = sub { |
|
168 |
my ($element, $index, $key, $sub) = @_; |
|
169 |
my $ref = ref $element; |
|
170 |
return $sub ? $sub->($element) |
|
171 |
: !$ref ? $element |
|
172 |
: $ref eq 'ARRAY' ? $element->[$index] |
|
173 |
: $ref eq 'HASH' ? $element->{$key} |
|
174 |
: $element->$key; |
|
175 |
}; |
|
176 |
|
|
177 |
my @elements = (); |
|
178 |
push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty}; |
|
179 |
push @elements, map [ |
|
180 |
$value_title_sub ? $value_title_sub->($_) : ( |
|
181 |
$access->($_, 0, $value_key, $value_sub), |
|
182 |
$access->($_, 1, $title_key, $title_sub), |
|
183 |
) |
|
184 |
], @{ $collection } if $collection && ref $collection eq 'ARRAY'; |
|
175 | 185 |
|
176 | 186 |
my $code = ''; |
177 | 187 |
foreach my $result (@elements) { |
... | ... | |
306 | 316 |
C<$value> and with arbitrary HTML attributes from C<%attributes>. The |
307 | 317 |
tag's C<id> defaults to C<name_to_id($name)>. |
308 | 318 |
|
319 |
=item C<hidden_tag $name, $value, %attributes> |
|
320 |
|
|
321 |
Creates a HTML 'input type=hidden' tag named C<$name> with the value |
|
322 |
C<$value> and with arbitrary HTML attributes from C<%attributes>. The |
|
323 |
tag's C<id> defaults to C<name_to_id($name)>. |
|
324 |
|
|
325 |
=item C<submit_tag $name, $value, %attributes> |
|
326 |
|
|
327 |
Creates a HTML 'input type=submit class=submit' tag named C<$name> with the |
|
328 |
value C<$value> and with arbitrary HTML attributes from C<%attributes>. The |
|
329 |
tag's C<id> defaults to C<name_to_id($name)>. |
|
330 |
|
|
309 | 331 |
=item C<textarea_tag $name, $value, %attributes> |
310 | 332 |
|
311 | 333 |
Creates a HTML 'textarea' tag named C<$name> with the content |
... | ... | |
381 | 403 |
For cases 3 and 4 C<$options{value}> defaults to C<id> and |
382 | 404 |
C<$options{title}> defaults to C<$options{value}>. |
383 | 405 |
|
406 |
In addition to pure keys/method you can also provide coderefs as I<value_sub> |
|
407 |
and/or I<title_sub>. If present, these take precedence over keys or methods, |
|
408 |
and are called with the element as first argument. It must return the value or |
|
409 |
title. |
|
410 |
|
|
411 |
Lastly a joint coderef I<value_title_sub> may be provided, which in turn takes |
|
412 |
precedence over each individual sub. It will only be called once for each |
|
413 |
element and must return a list of value and title. |
|
414 |
|
|
384 | 415 |
If the option C<with_empty> is set then an empty element (value |
385 | 416 |
C<undef>) will be used as the first element. The title to display for |
386 | 417 |
this element can be set with the option C<empty_title> and defaults to |
Auch abrufbar als: Unified diff
options_for_select - subs übergeben
ausserdem mehr dokumentation.