6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
use Template; |
use Template; |
9 |
|
use List::Util qw/first/; |
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
68 |
|
|
69 |
Create output from in-memory data structure using Template Toolkit template. |
Create output from in-memory data structure using Template Toolkit template. |
70 |
|
|
71 |
my $text = $tt->apply( template => 'text.tt', data => @ds ); |
my $text = $tt->apply( |
72 |
|
template => 'text.tt', |
73 |
|
data => \@ds |
74 |
|
); |
75 |
|
|
76 |
|
It also has follwing template toolikit filter routies defined: |
77 |
|
|
78 |
=cut |
=cut |
79 |
|
|
88 |
$log->logconfess("need $a") unless ($args->{$a}); |
$log->logconfess("need $a") unless ($args->{$a}); |
89 |
} |
} |
90 |
|
|
91 |
|
=head3 tt_filter_type |
92 |
|
|
93 |
|
filter to return values of specified from @ds |
94 |
|
|
95 |
|
=cut |
96 |
|
|
97 |
|
sub tt_filter_type { |
98 |
|
my ($data,$type) = @_; |
99 |
|
|
100 |
|
die "no data?" unless ($data); |
101 |
|
$type ||= 'display'; |
102 |
|
|
103 |
|
my $default_delimiter = { |
104 |
|
'display' => '¶<br/>', |
105 |
|
'index' => '\n', |
106 |
|
}; |
107 |
|
|
108 |
|
return sub { |
109 |
|
|
110 |
|
my ($name,$join) = @_; |
111 |
|
|
112 |
|
die "no data array" unless ($data->{'data'} && ref($data->{'data'}) eq 'ARRAY'); |
113 |
|
# Hm? Should we die here? |
114 |
|
return unless ($name); |
115 |
|
|
116 |
|
my $item = first { $_->{'name'} eq $name } @{ $data->{'data'} }; |
117 |
|
|
118 |
|
return unless($item); |
119 |
|
|
120 |
|
my $v = $item->{$type} || return; |
121 |
|
|
122 |
|
if (ref($v) eq 'ARRAY') { |
123 |
|
if ($#{$v} == 0) { |
124 |
|
$v = $v->[0]; |
125 |
|
} else { |
126 |
|
$join = $default_delimiter->{$type} unless defined($join); |
127 |
|
$v = join($join, @{$v}); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
return $v; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
$args->{'d'} = tt_filter_type($args, 'display'); |
136 |
|
|
137 |
my $out; |
my $out; |
138 |
|
|
139 |
$self->{'tt'}->process( |
$self->{'tt'}->process( |
153 |
$tt->to_file( |
$tt->to_file( |
154 |
file => 'out.txt', |
file => 'out.txt', |
155 |
template => 'text.tt', |
template => 'text.tt', |
156 |
data => @ds |
data => \@ds |
157 |
); |
); |
158 |
|
|
159 |
=cut |
=cut |