summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-03-24 11:30:01 -0400
committerJesse Luehrs <doy@tozt.net>2014-03-24 11:30:01 -0400
commit416a8df32250db49244a273c331269b549425222 (patch)
treee5dc5c2c861027aaccae415f4c9aa5bea896bcc3
parent1f06a7f7efbef91939335b070c279ede002af32f (diff)
parentbb3ff3e1f924d530b3bfdf192849b5ac1b9aa5b2 (diff)
downloadox-view-tt-416a8df32250db49244a273c331269b549425222.tar.gz
ox-view-tt-416a8df32250db49244a273c331269b549425222.zip
Merge pull request #1 from mascip/mascip
Users can now send additional parameters to the template
-rw-r--r--lib/OX/View/TT.pm30
-rw-r--r--t/array_parameter.t92
-rw-r--r--t/data/array_parameter/templates/index.tt8
3 files changed, 124 insertions, 6 deletions
diff --git a/lib/OX/View/TT.pm b/lib/OX/View/TT.pm
index 5e11579..9f8697e 100644
--- a/lib/OX/View/TT.pm
+++ b/lib/OX/View/TT.pm
@@ -10,17 +10,28 @@ use Template;
package MyApp;
use OX;
+ has 'template_params' => (
+ block => sub {
+ my $s = shift;
+ return {
+ some_scalar => 'value',
+ some_array => ['one', 'two'],
+ };
+ },
+ );
+
+
+
has view => (
is => 'ro',
isa => 'OX::View::TT',
- dependencies => ['template_root'],
+ dependencies => ['template_root', 'template_params'],
);
=head1 DESCRIPTION
This is a very thin wrapper around L<Template> which exposes some OX
-functionality to your template files. Templates rendered with this class will
-have access to these additional variables:
+functionality to your template files. It can be passed a template_params dependency, containing variables that will be passed to the template. Templates rendered with this class will have access to these additional variables:
=over 4
@@ -69,9 +80,16 @@ has 'tt' => (
}
);
-sub _build_template_params {
+has template_params => (
+ is => 'ro',
+ isa => 'HashRef',
+ default => sub { {} },
+);
+
+sub _get_all_template_params {
my ($self, $r, $params) = @_;
return +{
+ %{ $self->template_params },
base => $r->script_name,
uri_for => sub { $r->uri_for(@_) },
m => $r->mapping,
@@ -92,7 +110,7 @@ sub render {
my $out = '';
$self->tt->process(
$template,
- $self->_build_template_params( $r, $params ),
+ $self->_get_all_template_params( $r, $params ),
\$out
) || confess $self->tt->error;
$out;
@@ -116,7 +134,7 @@ sub template {
confess("Must supply a 'template' parameter")
unless exists $params{template};
- return $self->render($r, $params{template});
+ return $self->render($r, $params{template}, \%params);
}
__PACKAGE__->meta->make_immutable;
diff --git a/t/array_parameter.t b/t/array_parameter.t
new file mode 100644
index 0000000..22533c2
--- /dev/null
+++ b/t/array_parameter.t
@@ -0,0 +1,92 @@
+#!/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'],
+ );
+
+ our $AUTOLOAD;
+ sub AUTOLOAD {
+ my $self = shift;
+ my ($r) = @_;
+ (my $template = $AUTOLOAD) =~ s/.*:://;
+ $template .= '.tt';
+ $self->render($r, $template, $r->mapping);
+ }
+ sub can { 1 }
+}
+
+{
+ package Foo;
+ use OX;
+
+ has template_root => (
+ is => 'ro',
+ isa => 'Str',
+ block => sub {
+ Path::Class::dir($FindBin::Bin)->subdir('data', 'array_parameter', 'templates')->stringify
+ },
+ );
+
+ has 'template_params' => (
+ block => sub {
+ my $s = shift;
+ return {
+ some_scalar => 'scalar',
+ some_array => ['one', 'two'],
+ other_array => ['four', 'five'],
+ };
+ },
+ );
+
+ has view => (
+ is => 'ro',
+ isa => 'OX::View::TT',
+ dependencies => ['template_root', 'template_params'],
+ );
+
+ has root => (
+ is => 'ro',
+ isa => 'Foo::Controller',
+ dependencies => ['view'],
+ );
+
+ router as {
+ route '/' => 'root.index', (
+ content => 'Hello world',
+ );
+ };
+}
+
+my $foo = Foo->new;
+my $view = $foo->view;
+isa_ok($view, 'OX::View::TT');
+isa_ok($view->tt, 'Template');
+
+test_psgi
+ app => $foo->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<p>scalar</p>\n\n<span>one</span>\n\n<span>two</span>\n\n\n<span>four</span>\n\n<span>five</span>\n\n", "array parameter was passed");
+ }
+ };
+
+done_testing;
diff --git a/t/data/array_parameter/templates/index.tt b/t/data/array_parameter/templates/index.tt
new file mode 100644
index 0000000..5454d04
--- /dev/null
+++ b/t/data/array_parameter/templates/index.tt
@@ -0,0 +1,8 @@
+<b>[% content %]</b>
+<p>[% some_scalar %]</p>
+[% FOREACH elem IN some_array %]
+<span>[% elem %]</span>
+[% END %]
+[% FOREACH elem IN other_array %]
+<span>[% elem %]</span>
+[% END %]