summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2011-09-29 04:00:25 -0500
committerJesse Luehrs <doy@tozt.net>2011-09-29 04:00:25 -0500
commit2b8b1208ad357d3e8558e9b34cb99e5a299887b8 (patch)
treeb1ddb9153c191d215048dbdb9a34fa123623dbac
parent25b489a34ad55818a7407ce32f3c89968a1d240f (diff)
downloadox-view-tt-2b8b1208ad357d3e8558e9b34cb99e5a299887b8.tar.gz
ox-view-tt-2b8b1208ad357d3e8558e9b34cb99e5a299887b8.zip
allow routing directly to the view
-rw-r--r--lib/OX/View/TT.pm11
-rw-r--r--t/data/route/templates/bar/foo.tt1
-rw-r--r--t/data/route/templates/bar/index.tt1
-rw-r--r--t/data/route/templates/foo/foo.tt1
-rw-r--r--t/data/route/templates/foo/index.tt1
-rw-r--r--t/route.t116
6 files changed, 131 insertions, 0 deletions
diff --git a/lib/OX/View/TT.pm b/lib/OX/View/TT.pm
index 33e5e7c..df6461a 100644
--- a/lib/OX/View/TT.pm
+++ b/lib/OX/View/TT.pm
@@ -51,6 +51,17 @@ sub render {
$out;
}
+sub template {
+ my $self = shift;
+ my ($r) = @_;
+
+ my %params = $r->mapping;
+ confess("Must supply a 'template' parameter")
+ unless exists $params{template};
+
+ return $self->render($r, $params{template}, {});
+}
+
__PACKAGE__->meta->make_immutable;
no Moose; 1;
diff --git a/t/data/route/templates/bar/foo.tt b/t/data/route/templates/bar/foo.tt
new file mode 100644
index 0000000..0b689dd
--- /dev/null
+++ b/t/data/route/templates/bar/foo.tt
@@ -0,0 +1 @@
+<p>[% uri_for(template => 'foo.tt') %]: [% my_thing %]</p>
diff --git a/t/data/route/templates/bar/index.tt b/t/data/route/templates/bar/index.tt
new file mode 100644
index 0000000..f9000e6
--- /dev/null
+++ b/t/data/route/templates/bar/index.tt
@@ -0,0 +1 @@
+<b>Hello world: [% my_thing %]</b>
diff --git a/t/data/route/templates/foo/foo.tt b/t/data/route/templates/foo/foo.tt
new file mode 100644
index 0000000..ba1c791
--- /dev/null
+++ b/t/data/route/templates/foo/foo.tt
@@ -0,0 +1 @@
+<p>[% uri_for(template => 'foo.tt') %]</p>
diff --git a/t/data/route/templates/foo/index.tt b/t/data/route/templates/foo/index.tt
new file mode 100644
index 0000000..7849703
--- /dev/null
+++ b/t/data/route/templates/foo/index.tt
@@ -0,0 +1 @@
+<b>Hello world</b>
diff --git a/t/route.t b/t/route.t
new file mode 100644
index 0000000..c4507eb
--- /dev/null
+++ b/t/route.t
@@ -0,0 +1,116 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Plack::Test;
+use FindBin;
+
+use HTTP::Request::Common;
+use Path::Class ();
+
+{
+ package Foo;
+ use OX;
+
+ has template_root => (
+ is => 'ro',
+ isa => 'Str',
+ block => sub {
+ Path::Class::dir($FindBin::Bin)->subdir('data', 'route', 'templates', 'foo')->stringify
+ },
+ );
+
+ has view => (
+ is => 'ro',
+ isa => 'OX::View::TT',
+ dependencies => ['template_root'],
+ );
+
+ router as {
+ route '/' => 'view.template', (
+ template => 'index.tt',
+ );
+ route '/foo' => 'view.template', (
+ template => 'foo.tt',
+ );
+ };
+}
+
+test_psgi
+ app => Foo->new->to_app,
+ client => sub {
+ my $cb = shift;
+
+ {
+ my $res = $cb->(GET 'http://localhost/');
+ is($res->code, 200, "right code");
+ is($res->content, "<b>Hello world</b>\n", "right content");
+ }
+ {
+ my $res = $cb->(GET 'http://localhost/foo');
+ is($res->code, 200, "right code");
+ is($res->content, "<p>/foo</p>\n", "right content");
+ }
+ };
+
+{
+ package Bar::View;
+ use Moose;
+
+ extends 'OX::View::TT';
+
+ around render => sub {
+ my $orig = shift;
+ my $self = shift;
+ my ($r, $template, $params) = @_;
+ $params->{my_thing} = 'BAR';
+ return $self->$orig(@_);
+ };
+}
+
+{
+ package Bar;
+ use OX;
+
+ has template_root => (
+ is => 'ro',
+ isa => 'Str',
+ block => sub {
+ Path::Class::dir($FindBin::Bin)->subdir('data', 'route', 'templates', 'bar')->stringify
+ },
+ );
+
+ has view => (
+ is => 'ro',
+ isa => 'Bar::View',
+ dependencies => ['template_root'],
+ );
+
+ router as {
+ route '/' => 'view.template', (
+ template => 'index.tt',
+ );
+ route '/foo' => 'view.template', (
+ template => 'foo.tt',
+ );
+ };
+}
+
+test_psgi
+ app => Bar->new->to_app,
+ client => sub {
+ my $cb = shift;
+
+ {
+ my $res = $cb->(GET 'http://localhost/');
+ is($res->code, 200, "right code");
+ is($res->content, "<b>Hello world: BAR</b>\n", "right content");
+ }
+ {
+ my $res = $cb->(GET 'http://localhost/foo');
+ is($res->code, 200, "right code");
+ is($res->content, "<p>/foo: BAR</p>\n", "right content");
+ }
+ };
+
+done_testing;