File Coverage

blib/lib/Dancer/Template/TemplateSandbox.pm
Criterion Covered Total %
statement 34 52 65.3
branch 10 30 33.3
condition 2 5 40.0
subroutine 7 7 100.0
pod 2 2 100.0
total 55 96 57.2


line stmt bran cond sub pod time code
1             package Dancer::Template::TemplateSandbox;
2              
3 3     3   222635 use strict;
  3         7  
  3         112  
4 3     3   16 use warnings;
  3         6  
  3         93  
5 3     3   3961 use Dancer::Config 'setting';
  3         225539  
  3         213  
6 3     3   30 use Dancer::FileUtils 'path';
  3         7  
  3         125  
7              
8 3     3   16 use base 'Dancer::Template::Abstract';
  3         7  
  3         2078  
9              
10             our $VERSION = '1.00';
11              
12             my %_template_config;
13              
14             sub init
15             {
16 2     2 1 864 my ( $self ) = @_;
17              
18 2 100       13 die "Template::Sandbox is needed by Dancer::Template::TemplateSandbox"
19             unless Dancer::ModuleLoader->load( 'Template::Sandbox' );
20              
21 1         15 %_template_config = (
22             open_delimiter => '<%',
23             close_delimiter => '%>',
24             template_toolkit_compat => 1,
25 1         9 %{$self->config},
26             );
27              
28 1 50       31 $_template_config{ template_root } = setting( 'views' )
29             if setting( 'views' );
30              
31              
32 1 50       22 if( $_template_config{ cache } )
33             {
34 0         0 my ( $cache_type, $cache_dir );
35              
36             $cache_dir = delete $_template_config{ cache_dir }
37 0 0       0 if $_template_config{ cache_dir };
38             $cache_type = delete $_template_config{ cache_type }
39 0 0       0 if $_template_config{ cache_type };
40 0   0     0 $cache_type ||= 'memory';
41              
42 0 0       0 if( $_template_config{ cache } eq 'cache_factory' )
    0          
43             {
44 0         0 my ( %options );
45              
46 0 0       0 die "Unable to load chosen template caching module: " .
47             "Cache::CacheFactory"
48             unless Dancer::ModuleLoader->load( 'Cache::CacheFactory' );
49              
50 0 0       0 if( $cache_type eq 'file' )
51             {
52 0         0 %options = (
53             storage => { 'file' => { cache_root => $cache_dir, }, },
54             );
55             }
56             else
57             {
58 0         0 %options = (
59             storage => { $cache_type => {}, },
60             );
61             }
62              
63 0         0 $_template_config{ cache } = Cache::CacheFactory->new( %options );
64             }
65             elsif( $_template_config{ cache } eq 'chi' )
66             {
67 0         0 my ( %options );
68              
69 0 0       0 die "Unable to load chosen template caching module: CHI"
70             unless Dancer::ModuleLoader->load( 'CHI' );
71              
72 0 0       0 if( $cache_type eq 'file' )
73             {
74 0         0 %options = (
75             driver => 'File',
76             root_dir => $cache_dir,
77             );
78             }
79             else
80             {
81 0         0 %options = (
82             driver => Dancer::ModuleLoader->class_from_setting(
83             $cache_type ),
84             );
85             }
86              
87 0         0 $_template_config{ cache } = CHI->new( %options );
88             }
89             else
90             {
91 0         0 die "Unknown template caching module: $_template_config{ cache }";
92             }
93             }
94             }
95              
96             sub render($$$)
97             {
98 3     3 1 1532 my ( $self, $template, $tokens ) = @_;
99 3         4 my ( $ts );
100              
101 3 100 66     77 die "'$template' is not a regular file"
102             if !ref($template) && (!-f $template);
103              
104 2         23 $ts = Template::Sandbox->new( %_template_config );
105 2         275 $ts->add_vars( $tokens );
106              
107 2 100       54 if( ref( $template ) )
108             {
109 1         2 $ts->set_template_string( ${$template} );
  1         6  
110             }
111             else
112             {
113             $template =~ s~^\Q$_template_config{template_root}/\E~~
114 1 50       6 if $_template_config{ template_root };
115 1         8 $ts->set_template( $template );
116             }
117              
118 2         3131 my $contentref = $ts->run();
119 2 50       348 return( $contentref ? ${$contentref} : undef );
  2         33  
120             }
121              
122             1;
123             __END__