| 1 | package POE::Component::Server::HTTPServer::TemplateHandler; | ||
| 2 | use strict; | ||
| 3 | use HTTP::Status; | ||
| 4 | use HTML::Template; | ||
| 5 | use HTML::Template::HashWrapper; | ||
| 6 | use POE::Component::Server::HTTPServer::Handler; | ||
| 7 | use base 'POE::Component::Server::HTTPServer::StaticHandler'; | ||
| 8 | |||
| 9 | sub handle_plainfile { | ||
| 10 | my $self = shift; | ||
| 11 | my $filepath = shift; | ||
| 12 | my $context = shift; | ||
| 13 | |||
| 14 | unless ( -f $filepath && -r _ ) { | ||
| 15 | $context->{error_message} = "No such resource"; | ||
| 16 | return H_CONT; | ||
| 17 | } | ||
| 18 | |||
| 19 | my $ctx_assn = HTML::Template::HashWrapper->new($context); | ||
| 20 | my $templ = | ||
| 21 | HTML::Template->new( filename => $filepath, | ||
| 22 | die_on_bad_params => 0, | ||
| 23 | # cache => 1, | ||
| 24 | associate => $ctx_assn, | ||
| 25 | global_vars => 0, | ||
| 26 | ); | ||
| 27 | $context->{response}->code( RC_OK ); | ||
| 28 | $context->{response}->content( $templ->output ); | ||
| 29 | return H_FINAL; | ||
| 30 | } | ||
| 31 | |||
| 32 | 1; | ||
| 33 | __END__ |