summaryrefslogtreecommitdiffstats
path: root/lib/OX/View/TT.pm
blob: 9f8697e1c7e4cc3942d39fcd42ff7b19da6017a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package OX::View::TT;
use Moose;
# ABSTRACT: View wrapper class for TT renderers

use MooseX::Types::Path::Class;
use Template;

=head1 SYNOPSIS

  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', 'template_params'],
  );

=head1 DESCRIPTION

This is a very thin wrapper around L<Template> which exposes some OX
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

=item C<base>

The base URL that this app is hosted at (C<SCRIPT_NAME>).

=item C<uri_for>

A function which forwards its arguments to the C<uri_for> method in
L<OX::Request>.

=item C<m>

The hashref of match variables for the current route (equivalent to the
C<mapping> method on the L<OX::Request> object).

=back

=cut

has 'template_root' => (
    is       => 'ro',
    isa      => 'Path::Class::Dir',
    coerce   => 1,
    required => 1,
);

has 'template_config' => (
    is      => 'ro',
    isa     => 'HashRef',
    lazy    => 1,
    default => sub { +{} },
);

has 'tt' => (
    is      => 'ro',
    isa     => 'Template',
    lazy    => 1,
    default => sub {
        my $self = shift;
        Template->new(
            INCLUDE_PATH => $self->template_root,
            %{ $self->template_config }
        )
    }
);

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,
        %{ $params || {} }
    }
}

=method C<< render($r, $template, $params) >>

Renders a template, and returns a string containing the contents. C<$r> is the
request object, C<$template> is the name of the template, and C<$params> are
extra variables to pass to the template.

=cut

sub render {
    my ($self, $r, $template, $params) = @_;
    my $out = '';
    $self->tt->process(
        $template,
        $self->_get_all_template_params( $r, $params ),
        \$out
    ) || confess $self->tt->error;
    $out;
}

=method C<< template($r) >>

This is an action method which can be used directly as a route endpoint:

  route '/about' => 'view.template', (
      template => 'about.tt',
  );

=cut

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}, \%params);
}

__PACKAGE__->meta->make_immutable;
no Moose;

=head1 BUGS

No known bugs.

Please report any bugs through RT: email
C<bug-ox-view-tt at rt.cpan.org>, or browse to
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OX-View-TT>.

=head1 SEE ALSO

L<OX>

=head1 SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc OX::View::TT

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/OX-View-TT>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/OX-View-TT>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=OX-View-TT>

=item * Search CPAN

L<http://search.cpan.org/dist/OX-View-TT>

=back

=cut

1;