| 1 | ##-*- Mode: CPerl -*- | ||
| 2 | |||
| 3 | ## File: DTA::CAB::Server::HTTP::Handler::Template.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: DTA::CAB::Server::HTTP::Handler class: Template-Toolkit templates | ||
| 6 | ##====================================================================== | ||
| 7 | |||
| 8 | package DTA::CAB::Server::HTTP::Handler::Template; | ||
| 9 | use DTA::CAB::Server::HTTP::Handler; | ||
| 10 | use HTTP::Status; | ||
| 11 | use HTTP::Date qw(); | ||
| 12 | use Template; | ||
| 13 | use Carp; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | our @ISA = qw(DTA::CAB::Server::HTTP::Handler); | ||
| 17 | |||
| 18 | ##-------------------------------------------------------------- | ||
| 19 | ## Aliases | ||
| 20 | BEGIN { | ||
| 21 | DTA::CAB::Server::HTTP::Handler->registerAlias( | ||
| 22 | 'DTA::CAB::Server::Server::HTTP::Handler::template' => __PACKAGE__, | ||
| 23 | 'template' => __PACKAGE__, | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | |||
| 27 | ##-------------------------------------------------------------- | ||
| 28 | ## $h = $class_or_obj->new(%options) | ||
| 29 | ## + options: | ||
| 30 | ## contentType => $mimeType, ##-- default: text/plain ; alt. e.g. 'text/plain; charset="UTF-8"' | ||
| 31 | ## src => $src, ##-- template source (filename, fh, or string ref) | ||
| 32 | ## config => \%ttconfig, ##-- see Template(3pm) | ||
| 33 | ## vars => \%vars, ##-- extra vars | ||
| 34 | ## tmpl => $template, ##-- Template object (created by prepare()) | ||
| 35 | sub new { | ||
| 36 | my $that = shift; | ||
| 37 | return bless { | ||
| 38 | src=>undef, | ||
| 39 | contentType=>undef, | ||
| 40 | config => { | ||
| 41 | INTERPOLATE => 1, | ||
| 42 | POST_CHOMP => 0, | ||
| 43 | EVAL_PERL => 1, | ||
| 44 | ABSOLUTE => 1, | ||
| 45 | RELATIVE => 1, | ||
| 46 | }, | ||
| 47 | vars => {}, | ||
| 48 | tmpl => undef, | ||
| 49 | @_, | ||
| 50 | }, ref($that)||$that; | ||
| 51 | } | ||
| 52 | |||
| 53 | ## $bool = $obj->prepare($srv) | ||
| 54 | sub prepare { | ||
| 55 | my ($h,$srv) = @_; | ||
| 56 | $h->{tmpl} = Template->new($h->{config}) | ||
| 57 | or $h->logconfess("could not create Template object: $Template::ERROR"); | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | ## $rsp = $h->run($server, $localPath, $clientConn, $httpRequest) | ||
| 62 | sub run { | ||
| 63 | my ($h,$srv,$path,$csock,$hreq) = @_; | ||
| 64 | |||
| 65 | my $vars = { h=>$h, srv=>$srv, path=>$path, csock=>$csock, hreq=>$hreq, src=>$h->{src}, %{$h->{vars}||{}} }; | ||
| 66 | my $tmpl = $h->{tmpl}; | ||
| 67 | my $data = ''; | ||
| 68 | |||
| 69 | $tmpl->process($h->{src}, $vars, \$data) | ||
| 70 | or return $h->cerror($csock, undef, 'template error: '.$tmpl->error); | ||
| 71 | |||
| 72 | return HTTP::Response->new(RC_OK, | ||
| 73 | status_message(RC_OK), | ||
| 74 | [ | ||
| 75 | ($h->{contentType} ? ('Content-Type' => $h->{contentType}) : qw()), | ||
| 76 | ], | ||
| 77 | $data); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | 1; ##-- be happy |