| 1 | package Eve::Template; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use parent qw(Eve::Class); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use open qw(:std :utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use charnames qw(:full); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Template; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Template::Stash::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | B<Eve::Template> - a template engine class. | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | my $template = Eve::Template->new( | ||||||||||
| 22 | path => '/some/include/path', | ||||||||||
| 23 | compile_path => '/some/compile/path', | ||||||||||
| 24 | expiration_interval => 60); | ||||||||||
| 25 | |||||||||||
| 26 | my $output = $template->process( | ||||||||||
| 27 | file => 'helloworld.html', | ||||||||||
| 28 | var_hash => $var_hash); | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | B<Eve::Template> is a template engine adapter class. It adapts well | ||||||||||
| 33 | known B<Template> package. | ||||||||||
| 34 | |||||||||||
| 35 | =head3 Constructor arguments | ||||||||||
| 36 | |||||||||||
| 37 | =over 4 | ||||||||||
| 38 | |||||||||||
| 39 | =item C<path> | ||||||||||
| 40 | |||||||||||
| 41 | a path to template files | ||||||||||
| 42 | |||||||||||
| 43 | =item C<compile_path> | ||||||||||
| 44 | |||||||||||
| 45 | a path where compiled template files will be stored | ||||||||||
| 46 | |||||||||||
| 47 | =item C<expiration_interval> | ||||||||||
| 48 | |||||||||||
| 49 | determines how long (in seconds) a template is keeping cached in | ||||||||||
| 50 | memory before checking if it is changed | ||||||||||
| 51 | |||||||||||
| 52 | =item C<var_hash> | ||||||||||
| 53 | |||||||||||
| 54 | an optional hash of variables that will be additionally made available | ||||||||||
| 55 | to all processed templates. | ||||||||||
| 56 | |||||||||||
| 57 | =back | ||||||||||
| 58 | |||||||||||
| 59 | =head3 Throws | ||||||||||
| 60 | |||||||||||
| 61 | =over 4 | ||||||||||
| 62 | |||||||||||
| 63 | =item C<Eve::Error::Template> | ||||||||||
| 64 | |||||||||||
| 65 | when the template creation was unsuccessful. | ||||||||||
| 66 | |||||||||||
| 67 | =back | ||||||||||
| 68 | |||||||||||
| 69 | =head1 METHODS | ||||||||||
| 70 | |||||||||||
| 71 | =head2 B<init()> | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 5 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 76 | 5 | my ($self, %arg_hash) = @_; | |||||||||
| 77 | 5 | Eve::Support::arguments( | |||||||||
| 78 | \%arg_hash, | ||||||||||
| 79 | my ($path, $compile_path, $expiration_interval), | ||||||||||
| 80 | my $var_hash = {}); | ||||||||||
| 81 | |||||||||||
| 82 | 5 | $self->{'template'} = Template->new({ | |||||||||
| 83 | INCLUDE_PATH => $path, | ||||||||||
| 84 | COMPILE_DIR => $compile_path, | ||||||||||
| 85 | STAT_TTL => $expiration_interval, | ||||||||||
| 86 | ENCODING => 'utf8', | ||||||||||
| 87 | STASH => Template::Stash::XS->new($var_hash)}); | ||||||||||
| 88 | |||||||||||
| 89 | 5 | ▶ | if (not defined $self->{'template'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | Eve::Error::Template->throw(message => $Template::ERROR); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 4 | return; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 B<process()> | ||||||||||
| 97 | |||||||||||
| 98 | Process the template. | ||||||||||
| 99 | |||||||||||
| 100 | =head3 Arguments | ||||||||||
| 101 | |||||||||||
| 102 | =over 4 | ||||||||||
| 103 | |||||||||||
| 104 | =item C<file> | ||||||||||
| 105 | |||||||||||
| 106 | a template file to process | ||||||||||
| 107 | |||||||||||
| 108 | =item C<var_hash> | ||||||||||
| 109 | |||||||||||
| 110 | a variables hash to be used in the template. | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | =head3 Returns | ||||||||||
| 115 | |||||||||||
| 116 | A text assembled after processing. | ||||||||||
| 117 | |||||||||||
| 118 | =head3 Throws | ||||||||||
| 119 | |||||||||||
| 120 | =over 4 | ||||||||||
| 121 | |||||||||||
| 122 | =item C<Eve::Error::Template> | ||||||||||
| 123 | |||||||||||
| 124 | when the processing was unsuccessful. | ||||||||||
| 125 | |||||||||||
| 126 | =back | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | 3 | ▶ | sub process { | ||||||||
|
Subroutine
process: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 131 | 3 | my ($self, %arg_hash) = @_; | |||||||||
| 132 | 3 | Eve::Support::arguments( | |||||||||
| 133 | \%arg_hash, my ($file, $var_hash) = (\undef, {})); | ||||||||||
| 134 | |||||||||||
| 135 | 3 | my $output; | |||||||||
| 136 | |||||||||||
| 137 | 3 | ▶ | if (not defined $self->template->process( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | $file, $var_hash, \$output, {'binmode' => ":utf8"})) { | ||||||||||
| 139 | 1 | Eve::Error::Template->throw(message => $self->template->error()); | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 2 | return $output; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | =head1 SEE ALSO | ||||||||||
| 146 | |||||||||||
| 147 | =over 4 | ||||||||||
| 148 | |||||||||||
| 149 | =item L<Eve::Class> | ||||||||||
| 150 | |||||||||||
| 151 | =item L<Eve::Exception> | ||||||||||
| 152 | |||||||||||
| 153 | =item L<Template> | ||||||||||
| 154 | |||||||||||
| 155 | =back | ||||||||||
| 156 | |||||||||||
| 157 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 158 | |||||||||||
| 159 | Copyright 2012 Igor Zinovyev. | ||||||||||
| 160 | |||||||||||
| 161 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 162 | under the terms of either: the GNU General Public License as published | ||||||||||
| 163 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 164 | |||||||||||
| 165 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 166 | |||||||||||
| 167 | |||||||||||
| 168 | =head1 AUTHOR | ||||||||||
| 169 | |||||||||||
| 170 | =over 4 | ||||||||||
| 171 | |||||||||||
| 172 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||
| 173 | |||||||||||
| 174 | =back | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||
| 177 | |||||||||||
| 178 | 2 | 1; | |||||||||