File Coverage

blib/lib/Sledge/Template/Xslate.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Sledge::Template::Xslate;
2              
3 2     2   108093 use strict;
  2         4  
  2         91  
4 2     2   12 use warnings;
  2         4  
  2         134  
5              
6             our $VERSION = '0.09';
7             our $XSLATE_CACHE_DIR_NAME = 'xslate';
8              
9 2     2   2460 use parent qw(Sledge::Template);
  2         725  
  2         11  
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__