POE::Component::Server::HTTPServer::TemplateHandler - serve templated files


POE-Component-Server-HTTPServer documentation Contained in the POE-Component-Server-HTTPServer distribution.

Index


Code Index:

NAME

Top

POE::Component::Server::HTTPServer::TemplateHandler - serve templated files

SYNOPSIS

Top

  use POE::Component::Server::HTTPServer;
  my $server = POE::Component::Server::HTTPServer->new();
  $server->handlers([
                      '/tmpl' => MySetupHandler->new(),
                      '/tmpl' => new_handler('TemplateHandler', './tmpl'),
		    ]);

DESCIPTION

Top

TemplateHandler resolves requests to files in the file system, then interprets them as HTML::Template templates. The context is used for setting parameters for the template.

This is written primarily as an example of extending StaticHandler, and is not particularly robust.

SEE ALSO

Top

POE::Component::Server::HTTPServer, POE::Component::Server::HTTPServer::StaticHandler, HTML::Template

AUTHOR

Top

Greg Fast <gdf@speakeasy.net>

COPYRIGHT

Top


POE-Component-Server-HTTPServer documentation Contained in the POE-Component-Server-HTTPServer distribution.

package POE::Component::Server::HTTPServer::TemplateHandler;
use strict;
use HTTP::Status;
use HTML::Template;
use HTML::Template::HashWrapper;
use POE::Component::Server::HTTPServer::Handler;
use base 'POE::Component::Server::HTTPServer::StaticHandler';

sub handle_plainfile {
  my $self = shift;
  my $filepath = shift;
  my $context = shift;

  unless ( -f $filepath && -r _ ) {
    $context->{error_message} = "No such resource";
    return H_CONT;
  }

  my $ctx_assn = HTML::Template::HashWrapper->new($context);
  my $templ = 
    HTML::Template->new( filename => $filepath,
			 die_on_bad_params => 0,
			 # cache => 1,
			 associate => $ctx_assn,
			 global_vars => 0,
		       );
  $context->{response}->code( RC_OK );
  $context->{response}->content( $templ->output );
  return H_FINAL;
}

1;
__END__