Commit | Line | Data |
8077080c |
1 | package Catalyst::View::TT; |
2 | |
3 | use strict; |
4 | use base qw/Catalyst::Base Class::Data::Inheritable/; |
5 | use Template; |
6 | use Template::Timer; |
7 | use NEXT; |
8 | |
6bf01217 |
9 | our $VERSION = '0.06'; |
8077080c |
10 | |
11 | __PACKAGE__->mk_accessors('template'); |
12 | __PACKAGE__->mk_classdata('config'); |
13 | |
7b592fc7 |
14 | __PACKAGE__->config( { EVAL_PERL => 0 } ); |
8077080c |
15 | |
16 | =head1 NAME |
17 | |
18 | Catalyst::View::TT - Template View Class |
19 | |
20 | =head1 SYNOPSIS |
21 | |
22 | # use the helper |
23 | create view TT TT |
24 | |
25 | # lib/MyApp/View/TT.pm |
26 | package MyApp::View::TT; |
27 | |
28 | use base 'Catalyst::View::TT'; |
29 | |
30 | __PACKAGE__->config->{DEBUG} = 'all'; |
31 | |
32 | 1; |
33 | |
34 | $c->forward('MyApp::View::TT'); |
35 | |
36 | =head1 DESCRIPTION |
37 | |
7b592fc7 |
38 | This is the C<Template> view class. Your subclass should inherit |
39 | from this class. If you want to override TT config settings, you |
40 | can do it there by setting __PACKAGE__->config->{OPTION} as shown |
41 | in the synopsis. Of interest might be EVAL_PERL, which is disabled |
42 | by default, and LOAD_TEMPLATES, which is set to use the provider. |
43 | |
44 | If you want to use EVAL perl, add something like this: |
45 | |
46 | __PACKAGE__->config->{EVAL_PERL} = 1; |
47 | __PACKAGE__->config->{LOAD_TEMPLATES} = undef; |
8077080c |
48 | |
49 | =head2 OVERLOADED METHODS |
50 | |
51 | =cut |
52 | |
53 | sub new { |
54 | my $class = shift; |
55 | my $c = shift; |
56 | my $self = $class->NEXT::new(@_); |
7b592fc7 |
57 | our ($template, $provider); |
8077080c |
58 | my $root = $c->config->{root}; |
7b592fc7 |
59 | $provider ||= Template::Provider->new(); |
60 | $provider->include_path([ $root, "$root/base" ]); |
61 | my %config= ( LOAD_TEMPLATES => [ $provider ], |
62 | %{ $class->config() }, |
63 | INCLUDE_PATH => [ $root, "$root/base" ] |
64 | ); |
8077080c |
65 | $config{CONTEXT} = Template::Timer->new(%config) if $c->debug; |
7b592fc7 |
66 | $self->template( Template->new(\%config)); |
8077080c |
67 | return $self; |
68 | } |
69 | |
70 | =head3 process |
71 | |
72 | Renders the template specified in $c->stash->{template} or $c->request->match |
73 | to $c->response->output. |
74 | |
75 | =cut |
76 | |
77 | sub process { |
78 | my ( $self, $c ) = @_; |
79 | $c->res->headers->content_type('text/html;charset=utf8'); |
80 | my $output; |
81 | my $name = $c->stash->{template} || $c->req->match; |
82 | unless ($name) { |
83 | $c->log->debug('No template specified for rendering') if $c->debug; |
84 | return 0; |
85 | } |
86 | $c->log->debug(qq/Rendering template "$name"/) if $c->debug; |
87 | unless ( |
88 | $self->template->process( |
89 | $name, |
90 | { |
91 | %{ $c->stash }, |
92 | base => $c->req->base, |
93 | c => $c, |
94 | name => $c->config->{name} |
95 | }, |
96 | \$output |
97 | ) |
98 | ) |
99 | { |
100 | my $error = $self->template->error; |
101 | $error = qq/Couldn't render template "$error"/; |
102 | $c->log->error($error); |
103 | $c->errors($error); |
104 | } |
105 | $c->res->output($output); |
106 | return 1; |
107 | } |
108 | |
109 | =head3 config |
110 | |
111 | This allows your view subclass to pass additional settings to the |
112 | TT config hash. |
113 | |
114 | =cut |
115 | |
116 | =head1 SEE ALSO |
117 | |
118 | L<Catalyst>. |
119 | |
120 | =head1 AUTHOR |
121 | |
122 | Sebastian Riedel, C<sri@cpan.org> |
123 | Marcus Ramberg |
124 | |
125 | =head1 COPYRIGHT |
126 | |
127 | This program is free software, you can redistribute it and/or modify it under |
128 | the same terms as Perl itself. |
129 | |
130 | =cut |
131 | |
132 | 1; |