--- trunk/lib/WebPAC/Output/TT.pm 2005/07/17 22:28:11 21 +++ trunk/lib/WebPAC/Output/TT.pm 2005/11/15 14:31:12 62 @@ -6,6 +6,7 @@ use base qw/WebPAC::Common/; use Template; +use List::Util qw/first/; use Data::Dumper; =head1 NAME @@ -69,9 +70,11 @@ my $text = $tt->apply( template => 'text.tt', - data => @ds + data => \@ds ); +It also has follwing template toolikit filter routies defined: + =cut sub apply { @@ -85,6 +88,52 @@ $log->logconfess("need $a") unless ($args->{$a}); } +=head3 tt_filter_type + +filter to return values of specified from @ds + +=cut + + sub tt_filter_type { + my ($data,$type) = @_; + + die "no data?" unless ($data); + $type ||= 'display'; + + my $default_delimiter = { + 'display' => '¶
', + 'index' => '\n', + }; + + return sub { + + my ($name,$join) = @_; + + die "no data array" unless ($data->{'data'} && ref($data->{'data'}) eq 'ARRAY'); + # Hm? Should we die here? + return unless ($name); + + my $item = first { $_->{'name'} eq $name } @{ $data->{'data'} }; + + return unless($item); + + my $v = $item->{$type} || return; + + if (ref($v) eq 'ARRAY') { + if ($#{$v} == 0) { + $v = $v->[0]; + } else { + $join = $default_delimiter->{$type} unless defined($join); + $v = join($join, @{$v}); + } + } + + return $v; + } + } + + $args->{'d'} = tt_filter_type($args, 'display'); + my $out; $self->{'tt'}->process( @@ -104,7 +153,7 @@ $tt->to_file( file => 'out.txt', template => 'text.tt', - data => @ds + data => \@ds ); =cut