App-TemplateServer
view release on metacpan or search on metacpan
lib/App/TemplateServer.pm view on Meta::CPAN
my $res = eval {
given($req->uri){
when(m{^/(?:index(?:[.]html?)?)?$}){
return $self->_render_index($req);
}
when(m{^/favicon.ico$}){
return $self->_render_favicon($req);
}
default {
return $self->_render_template($req);
}
}
};
if($@ || !$res){
my $h = HTTP::Headers->new;
$res = HTTP::Response->new(500, 'Internal Server Error', $h, $@);
}
return $res;
};
sub _success {
my $content = shift;
my $headers = HTTP::Headers->new;
# set up utf8
$headers->header('content-type' => 'text/html; charset=utf8');
utf8::upgrade($content); # kill latin1
utf8::encode($content);
return HTTP::Response->new(200, 'OK', $headers, $content);
}
method _mk_context($req) {
return App::TemplateServer::Context->new(
data => $self->_data,
request => $req,
server => $self->_daemon,
);
};
method _render_template($req) {
my $context = $self->_mk_context($req);
my $template = uri_unescape($req->uri->path);
$template =~ s{^/}{};
my $content = $self->provider->render_template($template, $context);
return _success($content);
};
method _render_index($req) {
my $index = App::TemplateServer::Page::Index->new(
provider => $self->provider,
);
my $context = $self->_mk_context($req);
my $content = $index->render($context);
return _success($content);
};
method _render_favicon($req){
return HTTP::Response->new(404, 'Not found');
};
1;
__END__
=head1 NAME
App::TemplateServer - application to serve processed templates
=head1 SYNOPSIS
template-server --docroot project/templates --data project/test_data.yml
=head1 DESCRIPTION
Occasionally you need to give HTML templates to someone to edit
without setting up a full perl environment for them. You can use this
application to serve templates to the browser and provide those
templates with sample data to operate on. The template editor will
need Perl, but not a database, Apache, Catalyst, etc. (You can build
a PAR and then they won't need Perl either.)
It's also useful for experimenting with new templating engines. You
can start writing templates right away, without having to setup Apache
or a Catalyst application first. Interfacing C<App::TemplateServer>
to a new templating system is a quick matter of writing a few lines of
code. (See L<App::TemplateServer::Provider> for details.)
As a user, you'll be interacting with C<App::TemplateServer> via the
included C<template-server> script.
=head1 METHODS
=head2 run
Start the server. This method never returns.
=head1 ATTRIBUTES
=head2 port
The port to bind the server to. Defaults to 4000.
=head2 docroot
The directory containing templates. Defaults to the current
directory.
=head2 provider_class
The class name of the Provider to use. Defaults to
C<App::TemplateServer::Provider::TT>, but you can get others from the
CPAN (for using templating systems other than TT).
As of version 0.02, you can omit the
C<App::TemplateServer::Provider::> prefix if you prefer. The literal
class you pass will be loaded first; if that fails then the
C<App::TemplateServer::Provider::> prefix is added. Failing that, an
exception is thrown.
( run in 1.574 second using v1.01-cache-2.11-cpan-39bf76dae61 )