File Coverage

blib/lib/Dancer/Template/Ctpp2.pm
Criterion Covered Total %
statement 20 38 52.6
branch 1 12 8.3
condition 0 6 0.0
subroutine 7 9 77.7
pod 3 3 100.0
total 31 68 45.5


line stmt bran cond sub pod time code
1             package Dancer::Template::Ctpp2;
2              
3 2     2   108793 use strict;
  2         5  
  2         79  
4 2     2   11 use warnings;
  2         3  
  2         65  
5 2     2   2091 use Dancer::Config 'setting';
  2         211419  
  2         147  
6 2     2   21 use Dancer::ModuleLoader;
  2         4  
  2         44  
7 2     2   9 use Dancer::FileUtils 'path';
  2         3  
  2         85  
8              
9 2     2   11 use base 'Dancer::Template::Abstract';
  2         4  
  2         985  
10              
11             our $VERSION = '0.02';
12              
13             our $_ctpp2;
14             our %_cfg;
15              
16             sub default_tmpl_ext {
17 0 0   0 1 0 return ($_cfg{'use_bytecode'}) ? 'ct2' : 'tmpl';
18             }
19              
20              
21             sub init {
22 1     1 1 32 my ($self) = @_;
23              
24 1 50       10 die "HTML::CTPP2 is needed by Dancer::Template::Ctpp2"
25             unless Dancer::ModuleLoader->load('HTML::CTPP2');
26            
27 0           %_cfg=%{$self->config};
  0            
28 0 0         my $use_bytecode = (defined $_cfg{'compiled'}) ? delete $_cfg{'compiled'} : 0;
29              
30 0           $_ctpp2 = new HTML::CTPP2(
31             arg_stack_size => 1024,
32             code_stack_size => 1024,
33             steps_limit => 1024*1024,
34             max_functions => 1024,
35             %_cfg,
36             );
37            
38 0           $_cfg{'use_bytecode'} = $use_bytecode;
39             }
40              
41             sub render($$$) {
42 0     0 1   my ($self, $template, $tokens) = @_;
43              
44 0 0 0       die "'$template' is not a regular file"
45             if !ref($template) && (!-f $template);
46              
47 0           my $b;
48              
49 0 0         if ($_cfg{'use_bytecode'}) {
50 0           $b = $_ctpp2->load_bytecode($template);
51             } else {
52 0           $b = $_ctpp2->parse_template($template);
53             }
54              
55 0           $_ctpp2->reset();
56 0           $_ctpp2->param($tokens);
57              
58 0           my $result = $_ctpp2->output($b);
59            
60 0 0 0       if(length(setting('charset')) && lc setting('charset') eq 'utf-8') {
61 0           return pack "U0C*", unpack "C*", $result;
62             } else {
63 0           return $result;
64             }
65            
66             }
67              
68             1;
69             __END__