-
Notifications
You must be signed in to change notification settings - Fork 0
view.t
robario edited this page May 21, 2013
·
1 revision
package MyApp;
use Slack qw(App);
1;
package MyApp::Root;
use JSON::PP;
use Slack qw(Controller);
use Template;
sub prefix { return q{/}; }
action default => qr{(?<name>.+)\z} => sub {
res->stash->{name} = req->args->{name};
};
view json => { extention => 'json' } => sub {
state $json = JSON::PP->new->utf8->allow_nonref;
res->body( $json->encode( res->stash ) );
};
view html => { extention => qr{html?} } => sub {
my ( $app, $action, $view ) = @_;
state $tt = Template->new( { INCLUDE_PATH => $app->config->{appdir} . '/t' } );
my $template = $action->{controller}->prefix =~ s{\A/}{}r . $action->{name} . '.tt';
$tt->process( $template, { app => $app, req => req, %{ res->stash } }, \my $output ) or do { ... };
res->body($output);
if ( not length res->content_type ) {
res->content_type('text/html; charset=UTF-8');
}
};
package MyApp::Foo;
use Slack qw(Controller);
view baz => { extention => 'json' } => sub {
res->body('{}');
};
1;
__END__
package Test {
use HTTP::Request::Common qw(GET);
use HTTP::Status qw(:constants);
use Plack::Test qw(test_psgi);
use Test::More;
my $app = MyApp->new;
test_psgi $app->to_app, sub {
my $cb = shift;
my $res;
$res = $cb->( GET '/foo' );
is $res->content, q{}, 'no view';
$res = $cb->( GET '/foo.html' );
is $res->content, "<!DOCTYPE html>\n<title>foo</title>\n", 'html view';
$res = $cb->( GET '/foo.json' );
is $res->content, '{"name":"foo"}', 'json view';
$res = $cb->( GET '/foo/bar.html' );
is $res->content, "<!DOCTYPE html>\n<title>foo/bar</title>\n", 'inherit view';
$res = $cb->( GET '/foo/bar.json' );
is $res->content, '{}', 'override view';
return;
};
done_testing;
}