blib/lib/Sledge/Template/Xslate.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Sledge::Template::Xslate;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.09';
7 our $XSLATE_CACHE_DIR_NAME = 'xslate';
8  
9 2 ▶use parent qw(Sledge::Template);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 use Text::Xslate;
12 use File::Basename;
13 use File::Spec::Memoized;
14 use Memoize;
15  
16 memoize('create_xslate');
17 sub create_xslate{ Text::Xslate->new(@_) } # It is memoized!
18  
19 sub import {
20 my($class, $option) = @_;
21 my $pkg = caller(0);
22  
23 undef $option unless(defined($option) && ref($option) eq 'HASH');
24  
25 no strict 'refs';
26 *{"$pkg\::create_template"} = sub {
27 my($self, $file) = @_;
28 return $class->new($file, $self, $option);
29 };
30 }
31  
32 sub new {
33 my($class, $file, $page, $option) = @_;
34  
35 my $config = $page->create_config();
36  
37 my $_option = {
38 filename => $file,
39 path => [dirname($file), '.', '/'],
40 input_layer => ':encoding(euc-jp)',# Sledge's default encoding is euc-jp (not utf-8)
41 suffix => '.html',
42 type => 'html',
43 cache => 1
44 };
45  
46 if(defined($option)){
47 foreach my $key (keys(%$option)){
48 $_option->{$key} = $option->{$key};
49 }
50 }
51  
52 my $self = {
53 _options => $_option,
54 _params => {
55 config => $config,
56 r => $page->r,
57 session => $page->session,
58 }
59 };
60  
61 bless($self, $class);
62 }
63  
64 sub add_associate { Sledge::Exception::UnimplementedMethod->throw }
65 sub associate_namespace { Sledge::Exception::UnimplementedMethod->throw }
66  
67 sub output {
68 my $self = shift;
69 my $config = $self->{_options};
70 my $input = delete $config->{filename};
71 my $cache_dir = $self->{'_params'}->{'config'}->can('cache_dir') ?
72 $self->{'_params'}->{'config'}->cache_dir : undef;
73  
74 # Template file check
75 unless (ref($input) || -e $input) {
76 Sledge::Exception::TemplateNotFound->throw(
77 "No template file detected: $input",
78 );
79 }
80  
81 # Create object
82 my $template = create_xslate($config);
83  
84 # Render
85 return ((ref $input eq 'SCALAR') ?
86 $template->render_string($$input, $self->{_params}):
87 $template->render($input, $self->{_params})
88 ) or Sledge::Exception::TemplateParseError->throw($template->error);
89 }
90  
91 1;
92 __END__
↑ Summary