/[webpac2]/trunk/lib/WebPAC/Output/TT.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Output/TT.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/lib/WebPAC/Output/html.pm revision 1 by dpavlin, Sat Jun 25 20:23:23 2005 UTC trunk/lib/WebPAC/Output/TT.pm revision 70 by dpavlin, Sat Nov 19 23:48:24 2005 UTC
# Line 1  Line 1 
1  package WebPAC::Output::html;  package WebPAC::Output::TT;
2    
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6    use base qw/WebPAC::Common/;
7    
8    use Template;
9    use List::Util qw/first/;
10    use Data::Dumper;
11    
12  =head1 NAME  =head1 NAME
13    
14  WebPAC::Output::html - The great new WebPAC::Output::html!  WebPAC::Output::TT - use Template Toolkit to produce output
15    
16  =head1 VERSION  =head1 VERSION
17    
# Line 17  our $VERSION = '0.01'; Line 23  our $VERSION = '0.01';
23    
24  =head1 SYNOPSIS  =head1 SYNOPSIS
25    
26  Quick summary of what the module does.  Produce output using Template Toolkit.
27    
28  Perhaps a little code snippet.  =head1 FUNCTIONS
29    
30      use WebPAC::Output::html;  =head2 new
31    
32      my $foo = WebPAC::Output::html->new();  Create new instance.
     ...  
33    
34  =head1 EXPORT   my $tt = new WebPAC::Output::TT(
35            include_path => '/path/to/conf/output/tt',
36            filters => {
37                    filter_1 => sub { uc(shift) },
38            },
39     );
40    
41  A list of functions that can be exported.  You can delete this section  By default, Template Toolkit will C<EVAL_PERL> if included in templates.
 if you don't export anything, such as for a purely object-oriented module.  
42    
43  =head1 FUNCTIONS  =cut
44    
45  =head2 function1  sub new {
46            my $class = shift;
47            my $self = {@_};
48            bless($self, $class);
49    
50            my $log = $self->_get_logger;
51    
52            # create Template toolkit instance
53            $self->{'tt'} = Template->new(
54                    INCLUDE_PATH => $self->{'include_path'},
55                    FILTERS => $self->{'filter'},
56                    EVAL_PERL => 1,
57            );
58            
59            $log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'});
60    
61  =cut          $log->debug("filters defined: ",Dumper($self->{'filter'}));
62    
63  sub function1 {          $self ? return $self : return undef;
64  }  }
65    
66  =head2 function2  
67    =head2 apply
68    
69    Create output from in-memory data structure using Template Toolkit template.
70    
71     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    
80  sub function2 {  sub apply {
81            my $self = shift;
82    
83            my $args = {@_};
84    
85            my $log = $self->_get_logger();
86    
87            foreach my $a (qw/template data/) {
88                    $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' => '&#182;<br/>',
105                            'index' => '\n',
106                    };
107    
108                    return sub {
109    
110                            my ($name,$join) = @_;
111    
112                            die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
113                            # Hm? Should we die here?
114                            return unless ($name);
115    
116                            my $item = $data->{'data'}->{$name} || return;
117    
118                            my $v = $item->{$type} || return;
119    
120                            if (ref($v) eq 'ARRAY') {
121                                    if ($#{$v} == 0) {
122                                            $v = $v->[0];
123                                    } else {
124                                            $join = $default_delimiter->{$type} unless defined($join);
125                                            $v = join($join, @{$v});
126                                    }
127                            }
128    
129                            return $v;
130                    }
131            }
132    
133            $args->{'d'} = tt_filter_type($args, 'display');
134    
135            my $out;
136    
137            $self->{'tt'}->process(
138                    $args->{'template'},
139                    $args,
140                    \$out
141            ) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() );
142    
143            return $out;
144  }  }
145    
146  =head1 AUTHOR  =head2 to_file
147    
148  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>  Create output from in-memory data structure using Template Toolkit template
149    to a file.
150    
151     $tt->to_file(
152            file => 'out.txt',
153            template => 'text.tt',
154            data => $ds
155     );
156    
157    =cut
158    
159    sub to_file {
160            my $self = shift;
161    
162            my $args = {@_};
163    
164  =head1 BUGS          my $log = $self->_get_logger();
165    
166  Please report any bugs or feature requests to          my $file = $args->{'file'} || $log->logconfess("need file name");
 C<bug-webpac-output-html@rt.cpan.org>, or through the web interface at  
 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebPAC>.  
 I will be notified, and then you'll automatically be notified of progress on  
 your bug as I make changes.  
167    
168  =head1 ACKNOWLEDGEMENTS          $log->debug("creating file ",$file);
169    
170            open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!");
171            print $fh $self->output(
172                    template => $args->{'template'},
173                    data => $args->{'data'},
174            ) || $log->logdie("print: $!");
175            close($fh) || $log->logdie("close: $!");
176    
177            return 1;
178    }
179    
180    
181    =head1 AUTHOR
182    
183    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
184    
185  =head1 COPYRIGHT & LICENSE  =head1 COPYRIGHT & LICENSE
186    
# Line 70  under the same terms as Perl itself. Line 191  under the same terms as Perl itself.
191    
192  =cut  =cut
193    
194  1; # End of WebPAC::Output::html  1; # End of WebPAC::Output::TT

Legend:
Removed from v.1  
changed lines
  Added in v.70

  ViewVC Help
Powered by ViewVC 1.1.26