| 1 | package Catalyst::View::HTML::Template; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Catalyst::View'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use HTML::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Catalyst::View::HTML::Template - HTML::Template View Class | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # use the helper | ||||||||||||||||||||||||||||
| 17 | create.pl view HTML::Template HTML::Template | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # lib/MyApp/View/HTML/Template.pm | ||||||||||||||||||||||||||||
| 20 | package MyApp::View::HTML::Template; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use base 'Catalyst::View::HTML::Template'; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 25 | die_on_bad_params => 0, | ||||||||||||||||||||||||||||
| 26 | file_cache => 1, | ||||||||||||||||||||||||||||
| 27 | file_cache_dir => '/tmp/cache' | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Meanwhile, maybe in an 'end' action | ||||||||||||||||||||||||||||
| 33 | $c->forward('MyApp::View::HTML::Template'); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | This is the C<HTML::Template> view class. Your subclass should inherit from this | ||||||||||||||||||||||||||||
| 39 | class. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 METHODS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item process | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Renders the template specified in C<< $c->stash->{template} >> or C<< | ||||||||||||||||||||||||||||
| 48 | $c->request->match >>. | ||||||||||||||||||||||||||||
| 49 | Template params are set up from the contents of C<< $c->stash >>, | ||||||||||||||||||||||||||||
| 50 | augmented with C<base> set to C<< $c->req->base >> and C<name> to | ||||||||||||||||||||||||||||
| 51 | C<< $c->config->{name} >>. Output is stored in C<< $c->response->body >>. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $filename = $c->stash->{template} || $c->req->match; | ||||||||||||||||||||||||||
|
Condition0%0/3
$c->stash->{'template'} || $c->req->match
MC/DC0%0/2
$c->stash->{'template'} || $c->req->match
$c->stash->{'template'}
$c->req->match
Truth table0%0/3
A || B
A= $c->stash->{'template'}B= $c->req->match
| |||||||||||||||||||||||||||||
| 59 | 0 | my $body = $self->render($c,$filename); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ( $c->response->headers->content_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $c->res->headers->content_type('text/html; charset=utf-8'); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | $c->response->body($body); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item render | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Renders the given template and returns output. Template params are set up | ||||||||||||||||||||||||||||
| 73 | either from the contents of C<%$args> if $args is a hashref, or C<< $c->stash >>, | ||||||||||||||||||||||||||||
| 74 | augmented with C<base> set to C<< $c->req->base >> and C<name> to | ||||||||||||||||||||||||||||
| 75 | C<< $c->config->{name} >>. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 0 | my ( $self, $c, $filename, $args ) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | unless ($filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $c->log->debug('No template specified for rendering') if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | return 0; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my %options = ( | |||||||||||||||||||||||||||
| 88 | cache => 1, | ||||||||||||||||||||||||||||
| 89 | filename => $filename, | ||||||||||||||||||||||||||||
| 90 | path => [ $c->path_to('root'), $c->path_to('root','base') ], | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $c->log->debug(qq/Rendering template "$filename"/) if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $template = HTML::Template->new( %options, %{ $self } ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $template_params = $args && ref($args) eq 'HASH' ? $args : $c->stash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args and ref $args eq "HASH"
MC/DC0%0/2
$args and ref $args eq "HASH"
$args
ref $args eq "HASH"
Truth table0%0/3
A and B
A= $argsB= ref $args eq "HASH"
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | $template->param( | ||||||||||||||||||||||||||||
| 100 | base => $c->req->base, | ||||||||||||||||||||||||||||
| 101 | name => $c->config->{name}, | ||||||||||||||||||||||||||||
| 102 | 0 | %$template_params | |||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $output; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | eval { $output = $template->output }; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | chomp $error; | |||||||||||||||||||||||||||
| 111 | 0 | $error = qq/Couldn't render template "$filename". Error: "$error"/; | |||||||||||||||||||||||||||
| 112 | 0 | $c->log->error($error); | |||||||||||||||||||||||||||
| 113 | 0 | $c->error($error); | |||||||||||||||||||||||||||
| 114 | 0 | return 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | return $output; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item config | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | This allows your view subclass to pass additional settings to the | ||||||||||||||||||||||||||||
| 122 | HTML::Template config hash. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | L<HTML::Template>, L<Catalyst>, L<Catalyst::Base>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Christian Hansen, C<ch@ngmedia.com> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This program is free software, you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 137 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | 1; | |||||||||||||||||||||||||||