From 75719452d02d7afc2f2d1f6454ce9aba715a3db0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 3 Oct 2011 02:22:25 -0500 Subject: make mappings directly available to templates --- lib/OX/View/TT.pm | 1 + t/data/mapping/templates/index.tt | 1 + t/mapping.t | 72 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 t/data/mapping/templates/index.tt create mode 100644 t/mapping.t 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 @@ +[% m.foo %] [% m.bar %] 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, "FOO BAR\n", "right content"); + } + }; + +done_testing; -- cgit v1.2.3