Commit | Line | Data |
8077080c |
1 | package Catalyst::View::TT; |
2 | |
3 | use strict; |
caa61517 |
4 | use base qw/Catalyst::Base/; |
8077080c |
5 | use Template; |
6 | use Template::Timer; |
7 | use NEXT; |
8 | |
4687ac0d |
9 | our $VERSION = '0.08'; |
8077080c |
10 | |
11 | __PACKAGE__->mk_accessors('template'); |
8077080c |
12 | |
13 | =head1 NAME |
14 | |
15 | Catalyst::View::TT - Template View Class |
16 | |
17 | =head1 SYNOPSIS |
18 | |
19 | # use the helper |
caa61517 |
20 | create.pl view TT TT |
8077080c |
21 | |
22 | # lib/MyApp/View/TT.pm |
23 | package MyApp::View::TT; |
24 | |
25 | use base 'Catalyst::View::TT'; |
26 | |
27 | __PACKAGE__->config->{DEBUG} = 'all'; |
28 | |
29 | 1; |
30 | |
4687ac0d |
31 | # Meanwhile, maybe in an '!end' action |
8077080c |
32 | $c->forward('MyApp::View::TT'); |
33 | |
4687ac0d |
34 | |
8077080c |
35 | =head1 DESCRIPTION |
36 | |
4687ac0d |
37 | This is the C<Template> view class. Your subclass should inherit from this |
38 | class. The plugin renders the template specified in C<< $c->stash->{template} >> |
39 | or C<< $c->request->match >>. The template variables are set up from the |
40 | contents of C<< $c->stash >>, augmented with C<base> set to C<< $c->req->base >>, |
41 | C<c> to C<$c> and C<name> to C<< $c->config->{name} >>. The output is |
42 | stored in C<< $c->response->output >>. |
43 | |
44 | |
45 | If you want to override TT config settings, you can do it there by setting |
46 | C<< __PACKAGE__->config->{OPTION} >> as shown in the synopsis. Of interest might be |
47 | C<EVAL_PERL>, which is disabled by default, and C<LOAD_TEMPLATES>, which is set to |
48 | use the provider. |
7b592fc7 |
49 | |
50 | If you want to use EVAL perl, add something like this: |
51 | |
52 | __PACKAGE__->config->{EVAL_PERL} = 1; |
53 | __PACKAGE__->config->{LOAD_TEMPLATES} = undef; |
8077080c |
54 | |
4687ac0d |
55 | If you have configured Catalyst for debug output C<Catalyst::View::TT> will |
56 | enable profiling of template processing (using C<Template::Timer>. This will cause |
57 | HTML comments will get embedded in the output from your templates, such as: |
58 | |
59 | <!-- TIMER START: process mainmenu/mainmenu.ttml --> |
60 | <!-- TIMER START: include mainmenu/cssindex.tt --> |
61 | <!-- TIMER START: process mainmenu/cssindex.tt --> |
62 | <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) --> |
63 | <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) --> |
64 | |
65 | .... |
66 | |
67 | <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) --> |
68 | |
69 | You can supress template profiling when debug is enabled by setting: |
70 | |
71 | __PACKAGE__->config->{CONTEXT} = undef; |
72 | |
73 | |
caa61517 |
74 | =head2 METHODS |
8077080c |
75 | |
76 | =cut |
77 | |
78 | sub new { |
caa61517 |
79 | my $self = shift; |
80 | my $c = shift; |
81 | $self = $self->NEXT::new(@_); |
8077080c |
82 | my $root = $c->config->{root}; |
caa61517 |
83 | my %config = ( |
84 | EVAL_PERL => 0, |
85 | INCLUDE_PATH => [ $root, "$root/base" ], |
4e705c40 |
86 | %{ $self->config() } |
caa61517 |
87 | ); |
62728755 |
88 | |
c1c75b07 |
89 | if ( $c->debug && not exists $config{CONTEXT} ) { |
62728755 |
90 | $config{CONTEXT} = Template::Timer->new(%config); |
91 | } |
92 | |
caa61517 |
93 | $self->template( Template->new( \%config ) ); |
8077080c |
94 | return $self; |
95 | } |
96 | |
97 | =head3 process |
98 | |
4687ac0d |
99 | Renders the template specified in C<< $c->stash->{template} >> or C<< $c->request->match >>. |
100 | Template variables are set up from the contents of C<< $c->stash >>, augmented with C<base> |
101 | set to C<< $c->req->base >>, C<c> to C<$c> and C<name> to C<< $c->config->{name} >>. Output is |
102 | stored in C<< $c->response->output >>. |
8077080c |
103 | |
104 | =cut |
105 | |
106 | sub process { |
107 | my ( $self, $c ) = @_; |
108 | $c->res->headers->content_type('text/html;charset=utf8'); |
109 | my $output; |
110 | my $name = $c->stash->{template} || $c->req->match; |
111 | unless ($name) { |
112 | $c->log->debug('No template specified for rendering') if $c->debug; |
113 | return 0; |
114 | } |
115 | $c->log->debug(qq/Rendering template "$name"/) if $c->debug; |
116 | unless ( |
117 | $self->template->process( |
118 | $name, |
119 | { |
120 | %{ $c->stash }, |
121 | base => $c->req->base, |
122 | c => $c, |
123 | name => $c->config->{name} |
124 | }, |
125 | \$output |
126 | ) |
127 | ) |
128 | { |
129 | my $error = $self->template->error; |
130 | $error = qq/Couldn't render template "$error"/; |
131 | $c->log->error($error); |
132 | $c->errors($error); |
133 | } |
134 | $c->res->output($output); |
135 | return 1; |
136 | } |
137 | |
138 | =head3 config |
139 | |
140 | This allows your view subclass to pass additional settings to the |
141 | TT config hash. |
142 | |
143 | =cut |
144 | |
145 | =head1 SEE ALSO |
146 | |
147 | L<Catalyst>. |
148 | |
149 | =head1 AUTHOR |
150 | |
151 | Sebastian Riedel, C<sri@cpan.org> |
d938377b |
152 | Marcus Ramberg, C<mramberg@cpan.org> |
8077080c |
153 | |
154 | =head1 COPYRIGHT |
155 | |
156 | This program is free software, you can redistribute it and/or modify it under |
157 | the same terms as Perl itself. |
158 | |
159 | =cut |
160 | |
161 | 1; |