File Coverage

blib/lib/Ark/View/MT.pm
Criterion Covered Total %
statement 23 25 92.0
branch 2 4 50.0
condition 4 6 66.6
subroutine 7 9 77.7
pod 0 3 0.0
total 36 47 76.6


line stmt bran cond sub pod time code
1             package Ark::View::MT;
2 2     2   1237 use strict;
  2         4  
  2         74  
3 2     2   11 use warnings;
  2         4  
  2         78  
4 2     2   9 use Ark 'View';
  2         4  
  2         25  
5              
6             has include_path => (
7             is => 'rw',
8             isa => 'ArrayRef',
9             lazy => 1,
10             default => sub {
11             my $self = shift;
12             [$self->path_to('root')];
13             },
14             );
15              
16             has extension => (
17             is => 'rw',
18             isa => 'Str',
19             default => '.mt',
20             );
21              
22             has use_cache => (
23             is => 'rw',
24             isa => 'Bool',
25             default => 1,
26             );
27              
28             has cache => (
29             is => 'rw',
30             isa => 'HashRef',
31             lazy => 1,
32             default => sub { {} },
33             );
34              
35             has open_layer => (
36             is => 'rw',
37             isa => 'Str',
38             default => ':utf8',
39             );
40              
41             has macro => (
42             is => 'rw',
43             isa => 'HashRef',
44             lazy => 1,
45             default => sub { {} },
46             );
47              
48             has options => (
49             is => 'rw',
50             isa => 'HashRef',
51             lazy => 1,
52             default => sub { {} },
53             );
54              
55             has mt => (
56             is => 'rw',
57             isa => 'Text::MicroTemplate::Extended',
58             lazy => 1,
59             default => sub {
60             my $self = shift;
61             my $c = sub { $self->context };
62             my $stash = sub { $self->context->stash };
63              
64             $self->ensure_class_loaded('Text::MicroTemplate::Extended');
65             Text::MicroTemplate::Extended->new(
66             package_name => ref($self) . '::_MT',
67             include_path => $self->include_path,
68             extension => $self->extension,
69             use_cache => $self->use_cache,
70             open_layer => $self->open_layer,
71             macro => {
72 0     0     raw_string => sub($) { Text::MicroTemplate::EncodedString->new($_[0]) },
73             %{ $self->macro },
74             },
75             template_args => {
76             c => $c,
77             s => $stash,
78             stash => $stash,
79             },
80             %{ $self->options }
81             );
82             },
83             );
84              
85             sub template {
86 1     1 0 3 my ($self, $template) = @_;
87 1         10 $self->context->stash->{__view_mt_template} = $template;
88 1         3 $self;
89             }
90              
91             sub render {
92 4     4 0 93 my $self = shift;
93 4         7 my $template = shift;
94              
95 4 50 66     45 $template ||= $self->context->stash->{__view_mt_template}
      66        
96             || $self->context->request->action->reverse
97             or return;
98              
99 4         103 my $form_renderer = \&Ark::Form::render;
100 2     2   12 no warnings 'redefine';
  2         2  
  2         310  
101             local *Ark::Form::render = sub {
102 0     0   0 Text::MicroTemplate::EncodedString->new( $form_renderer->(@_) );
103 4 50       29 } if $form_renderer;
104              
105 4         37 $self->mt->render($template, @_);
106             }
107              
108             sub process {
109 3     3 0 6 my ($self, $c) = @_;
110 3         24 $c->response->body( $self->render );
111             }
112              
113             __PACKAGE__->meta->make_immutable;