From ee61dd2b559ab9d6bf741086c86fa9076efba022 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 11 Jan 2010 11:17:13 +0100 Subject: [PATCH] =?utf8?q?Neue=20Option=20'with=5Fempty'=20f=C3=BCr=20opti?= =?utf8?q?ons=5Ffor=5Fselect.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Sorgt dafür, dass das erste Element ein leeres Element ist. --- SL/Template/Plugin/L.pm | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/SL/Template/Plugin/L.pm b/SL/Template/Plugin/L.pm index 4f5632b9d..692c39663 100644 --- a/SL/Template/Plugin/L.pm +++ b/SL/Template/Plugin/L.pm @@ -107,7 +107,9 @@ sub options_for_select { my $value_key = $options{value} || 'id'; my $title_key = $options{title} || $value_key; - my @tags = (); + my @elements = (); + push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty}; + if ($collection && (ref $collection eq 'ARRAY')) { foreach my $element (@{ $collection }) { my @result = !ref $element ? ( $element, $element ) @@ -115,14 +117,19 @@ sub options_for_select { : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} ) : ( $element->$value_key, $element->$title_key ); - my %attributes = ( value => $result[0] ); - $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result[0] || '')); - - push @tags, $self->html_tag('option', _H($result[1]), %attributes); + push @elements, \@result; } } - return join('', @tags); + my $code = ''; + foreach my $result (@elements) { + my %attributes = ( value => $result->[0] ); + $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || '')); + + $code .= $self->html_tag('option', _H($result->[1]), %attributes); + } + + return $code; } 1; @@ -240,6 +247,11 @@ respectively. For cases 3 and 4 C<$options{value}> defaults to C and C<$options{title}> defaults to C<$options{value}>. +If the option C is set then an empty element (value +C) will be used as the first element. The title to display for +this element can be set with the option C and defaults to +an empty string. + =back =head1 MODULE AUTHORS -- 2.20.1