Skip to content

Commit

Permalink
More getting ready for release
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed Jan 31, 2011
1 parent f463109 commit abe9a11
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 75 deletions.
6 changes: 3 additions & 3 deletions Makefile.PL
Expand Up @@ -6,9 +6,9 @@ WriteMakefile(
'NAME' => 'Plack::App::TemplateToolkit',
'VERSION_FROM' => 'lib/Plack/App/TemplateToolkit.pm',
'PREREQ_PM' => {
'Template' => 0,
'Plack' => 0.9901,
'Test::More' => 0,
'Template' => 0,
'Plack' => 0.9901,
'Test::More' => 0,
'HTTP::Request' => 0,
}
);
Expand Down
45 changes: 11 additions & 34 deletions app.psgi
Expand Up @@ -5,27 +5,13 @@ use warnings;
use lib qw(lib);
use Plack::App::Cascade;
use Plack::Builder;
use Plack::App::URLMap;
use Plack::Middleware::Static;
use Plack::Middleware::ErrorDocument;
use Plack::App::TemplateToolkit;

my $root = '/Users/leo/svn/london-pm/LPM/root';

# Just to show that we can cascade to another bespoke app:
my $restriced_app = sub {
my $env = shift;
if ( $env->{PATH_INFO} eq '/test_403' ) {
return [
403, [ 'Content-Type' => 'text/plain' ],
["You should not be here"]
];
} else {

# Let something else handle it
return [ 404, [], [] ];
}
};

# Create our TT app, specifying the root and file extensions
my $tt_app = Plack::App::TemplateToolkit->new(
root => $root, # required
Expand All @@ -34,38 +20,29 @@ my $tt_app = Plack::App::TemplateToolkit->new(

# Create a cascade
my $cascade = Plack::App::Cascade->new;
# You could have your own app
# $cascade->add($app);
# Fall back to the TT app
$cascade->add($tt_app);
$cascade->add($restriced_app);

my $app = builder {
mount '/' => $cascade;
};
my $urlmap = Plack::App::URLMap->new;
$urlmap->map( "/" => $cascade );

my $app = $urlmap->to_app;

$app = Plack::Middleware::ErrorDocument->wrap(
$app,

# Does not work????
404 => "/page_not_found.html",
subrequest => 1
404 => "$root/page_not_found.html",
);

# Binary files can be served directly
$app = Plack::Middleware::Static->wrap(
$app,
path => qr{[gif|png|jpg|swf|ico|mov|mp3|pdf]$},
path => qr{[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$},
root => $root
);

# So can .js and .css files
$app = Plack::Middleware::Static->wrap(
$app,
path => qr{[js|css]$},
root => $root
);


# Plack::Middleware::Deflater might be good to use here

builder {
return builder {
$app;
}
55 changes: 30 additions & 25 deletions lib/Plack/App/TemplateToolkit.pm
Expand Up @@ -39,7 +39,6 @@ sub call {
if ( my $res = $self->_handle_tt($env) ) {
return $res;
}

return [ 404, [ 'Content-Type' => 'text/html' ], ['404 Not Found'] ];
}

Expand All @@ -58,20 +57,10 @@ sub _handle_tt {
}

if ( my $extension = $self->extension() ) {
return unless $path =~ /${extension}$/;
warn "Returning";
return 0 unless $path =~ /${extension}$/;
}

# Not needed because suggesting mount?
# if ( my $path_match = $self->path ) {
# for ($path) {
# my $matched
# = 'CODE' eq ref $path_match
# ? $path_match->($_)
# : $_ =~ $path_match;
# return unless $matched;
# }
# }


my $tt = $self->tt();

my $req = Plack::Request->new($env);
Expand Down Expand Up @@ -114,21 +103,34 @@ Plack::App::TemplateToolkit - Basic Template Toolkit
=head1 SYNOPSIS
# in app.psgi
use Plack::Builder;
# in app.psgi
use Plack::Builder;
use Plack::App::TemplateToolkit;
my $root = '/path/to/htdocs/';
my $tt_app = Plack::App::TemplateToolkit->new(
root => $root, # Required
)->to_app();
builder {
enable "Plack::Middleware::TemplateToolkit",
root => '/path/to/templates/, # required
path => '/tt/', # optional
extenstion => '.tt', # optional
content_type => 'text/html', # default, sets Content-Type header out
$app;
};
return builder {
# Page to show when requested file is missing
enable "Plack::Middleware::ErrorDocument",
404 => "$root/page_not_found.html";
# These files can be served directly
enable "Plack::Middleware::Static",
path => qr{[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$},
root => $root;
# Our application
$tt_app;
}
=head1 DESCRIPTION
Plack::Middleware::TemplateToolkit - process files through L<Template> Toolkit (TT)
Plack::App::TemplateToolkit - process files through L<Template> Toolkit (TT)
The idea behind this module is to provide access to L<Template> Toolkit (TT) for
content that is ALMOST static, but where having the power of TT can make
Expand All @@ -142,6 +144,9 @@ look at a propper framework such as L<Catalyst> if you do want to use them:
[% params.get('field') %] params is a L<Hash::MultiValue>
You can mix this application with other Plack::App applications which
you will find on CPAN.
=head1 CONFIGURATIONS
=over 4
Expand Down
1 change: 1 addition & 0 deletions t/root/index.html
@@ -0,0 +1 @@
Page [% SET a = 'value' %][% a %]
33 changes: 20 additions & 13 deletions t/tt.t
Expand Up @@ -3,22 +3,26 @@ use Plack::Test;
use Plack::Builder;
use Plack::App::TemplateToolkit;
use HTTP::Request;
use Path::Class;

my $app = sub {
return [ 200, [ 'Content-Type' => 'text/plain' ], ["DEFAULT"] ];
};
my $root = dir( file($0)->dir(), 'root' )->stringify();

my $app = Plack::App::TemplateToolkit->new(
root => $root, # Required
)->to_app();

my @tests = (
{ name => 'Basic request',
request_method => 'GET',
request_url => '/foo',
app => $default_app,
options => {
root => '/tmp/',
},
headers_out => {
'Content-Type' => 'text/html',
},
request_url => '/index.html',
content => 'Page value',
headers_out => { 'Content-Type' => 'text/html', },
},
{ name => 'Index request',
request_method => 'GET',
request_url => '/',
content => 'Page value',
headers_out => { 'Content-Type' => 'text/html', },
},

);
Expand All @@ -27,8 +31,6 @@ foreach my $test (@tests) {

pass( '---- ' . $test->{name} . ' ----' );
my $handler = builder {
enable "Plack::App::TemplateToolkit",
%{ $test->{options} };
$app;
};

Expand All @@ -41,6 +43,11 @@ foreach my $test (@tests) {
$test->{request_url} );
my $res = $cb->($req);

if ( $test->{content} ) {
is( $res->content(), $test->{content},
"Got content as expected" );
}

my $h = $res->headers();

while ( my ( $header, $value ) = each %{ $test->{headers_out} } ) {
Expand Down

0 comments on commit abe9a11

Please sign in to comment.