diff options
author | Jesse Luehrs <doy@tozt.net> | 2011-10-03 02:22:25 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2011-10-03 02:22:25 -0500 |
commit | 75719452d02d7afc2f2d1f6454ce9aba715a3db0 (patch) | |
tree | d916d91bb1bd48aa2b2ef8a92dc1a608c2b498f7 | |
parent | 652f66cf59308f901892457f778f7ad2e26411a6 (diff) | |
download | ox-view-tt-75719452d02d7afc2f2d1f6454ce9aba715a3db0.tar.gz ox-view-tt-75719452d02d7afc2f2d1f6454ce9aba715a3db0.zip |
make mappings directly available to templates
-rw-r--r-- | lib/OX/View/TT.pm | 1 | ||||
-rw-r--r-- | t/data/mapping/templates/index.tt | 1 | ||||
-rw-r--r-- | t/mapping.t | 72 |
3 files changed, 74 insertions, 0 deletions
diff --git a/lib/OX/View/TT.pm b/lib/OX/View/TT.pm index df6461a..69bfa91 100644 --- a/lib/OX/View/TT.pm +++ b/lib/OX/View/TT.pm @@ -36,6 +36,7 @@ sub _build_template_params { return +{ base => $r->script_name, uri_for => sub { $r->uri_for(@_ == 1 ? $_[0] : { @_ }) }, + m => { $r->mapping }, %{ $params || {} } } } diff --git a/t/data/mapping/templates/index.tt b/t/data/mapping/templates/index.tt new file mode 100644 index 0000000..d568d80 --- /dev/null +++ b/t/data/mapping/templates/index.tt @@ -0,0 +1 @@ +<b>[% m.foo %] [% m.bar %]</b> diff --git a/t/mapping.t b/t/mapping.t new file mode 100644 index 0000000..efab2f6 --- /dev/null +++ b/t/mapping.t @@ -0,0 +1,72 @@ +#!/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::Controller; + use Moose; + + has view => ( + is => 'ro', + isa => 'OX::View::TT', + required => 1, + handles => ['render'], + ); + + sub index { + my $self = shift; + my ($r) = @_; + $self->render($r, 'index.tt', {}); + } +} + +{ + package Foo; + use OX; + + has template_root => ( + is => 'ro', + isa => 'Str', + block => sub { + Path::Class::dir($FindBin::Bin)->subdir('data', 'mapping', 'templates')->stringify + }, + ); + + has view => ( + is => 'ro', + isa => 'OX::View::TT', + dependencies => ['template_root'], + ); + + has root => ( + is => 'ro', + isa => 'Foo::Controller', + dependencies => ['view'], + ); + + router as { + route '/' => 'root.index', ( + foo => 'FOO', + bar => 'BAR', + ); + }; +} + +test_psgi + app => Foo->new->to_app, + client => sub { + my $cb = shift; + + { + my $res = $cb->(GET 'http://localhost/'); + is($res->content, "<b>FOO BAR</b>\n", "right content"); + } + }; + +done_testing; |