From 93bae7aa5512c8d0f1bb78dbb800804f9cb139de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 27 Dec 2010 13:45:10 +0100 Subject: [PATCH] =?utf8?q?options=5Ffor=5Fselect=20-=20subs=20=C3=BCbergeb?= =?utf8?q?en?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit ausserdem mehr dokumentation. --- SL/Template/Plugin/L.pm | 63 ++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/SL/Template/Plugin/L.pm b/SL/Template/Plugin/L.pm index d61822f43..aaff9ef20 100644 --- a/SL/Template/Plugin/L.pm +++ b/SL/Template/Plugin/L.pm @@ -152,26 +152,36 @@ sub submit_tag { } sub options_for_select { - my $self = shift; - my $collection = shift; - my %options = _hashify(@_); + my $self = shift; + my $collection = shift; + my %options = _hashify(@_); - my $value_key = $options{value} || 'id'; - my $title_key = $options{title} || $value_key; + my $value_key = $options{value} || 'id'; + my $title_key = $options{title} || $value_key; - my @elements = (); - push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty}; + my $value_sub = $options{value_sub}; + my $title_sub = $options{title_sub}; - if ($collection && (ref $collection eq 'ARRAY')) { - foreach my $element (@{ $collection }) { - my @result = !ref $element ? ( $element, $element ) - : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] ) - : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} ) - : ( $element->$value_key, $element->$title_key ); + my $value_title_sub = $options{value_title_sub}; - push @elements, \@result; - } - } + my $access = sub { + my ($element, $index, $key, $sub) = @_; + my $ref = ref $element; + return $sub ? $sub->($element) + : !$ref ? $element + : $ref eq 'ARRAY' ? $element->[$index] + : $ref eq 'HASH' ? $element->{$key} + : $element->$key; + }; + + my @elements = (); + push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty}; + push @elements, map [ + $value_title_sub ? $value_title_sub->($_) : ( + $access->($_, 0, $value_key, $value_sub), + $access->($_, 1, $title_key, $title_sub), + ) + ], @{ $collection } if $collection && ref $collection eq 'ARRAY'; my $code = ''; foreach my $result (@elements) { @@ -306,6 +316,18 @@ Creates a HTML 'input type=text' tag named C<$name> with the value C<$value> and with arbitrary HTML attributes from C<%attributes>. The tag's C defaults to C. +=item C + +Creates a HTML 'input type=hidden' tag named C<$name> with the value +C<$value> and with arbitrary HTML attributes from C<%attributes>. The +tag's C defaults to C. + +=item C + +Creates a HTML 'input type=submit class=submit' tag named C<$name> with the +value C<$value> and with arbitrary HTML attributes from C<%attributes>. The +tag's C defaults to C. + =item C Creates a HTML 'textarea' tag named C<$name> with the content @@ -381,6 +403,15 @@ respectively. For cases 3 and 4 C<$options{value}> defaults to C and C<$options{title}> defaults to C<$options{value}>. +In addition to pure keys/method you can also provide coderefs as I +and/or I. If present, these take precedence over keys or methods, +and are called with the element as first argument. It must return the value or +title. + +Lastly a joint coderef I may be provided, which in turn takes +precedence over each individual sub. It will only be called once for each +element and must return a list of value and title. + 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 -- 2.20.1