File Coverage

blib/lib/Dancer2/Template/Mason.pm
Criterion Covered Total %
statement 19 20 95.0
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 24 27 88.8


line stmt bran cond sub pod time code
1             package Dancer2::Template::Mason;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Mason wrapper for Dancer2
4             $Dancer2::Template::Mason::VERSION = '0.1.2';
5 2     2   292556 use strict;
  2         3  
  2         61  
6 2     2   7 use warnings;
  2         4  
  2         94  
7              
8 2     2   1310 use HTML::Mason::Interp;
  2         182267  
  2         144  
9              
10 2     2   1608 use Moo;
  2         13234  
  2         11  
11              
12             require FindBin;
13              
14             with 'Dancer2::Core::Role::Template';
15              
16             has _engine => (
17             is => 'ro',
18             lazy => 1,
19             default => sub {
20             my %config = %{$_[0]->config || {}};
21              
22             delete @config{qw/ environment location extension /};
23              
24             HTML::Mason::Interp->new( %config );
25             },
26             );
27              
28             has _root_dir => (
29             is => 'rw',
30             lazy => 1,
31             default => sub {
32             my $self = shift;
33              
34             $self->config->{comp_root} ||=
35             $self->settings->{views} || $FindBin::Bin . '/views';
36             },
37             );
38              
39 0     0   0 sub _build_name { 'Dancer2::Template::Mason' }
40              
41             has '+default_tmpl_ext' => (
42             is => 'ro',
43             lazy => 1,
44             default => sub {
45             $_[0]->config->{extension} || 'mason';
46             },
47             );
48              
49             sub render {
50 1     1 0 254151 my ($self, $template, $tokens) = @_;
51              
52 1         23 my $root_dir = $self->_root_dir;
53            
54 1         62 $template =~ s/^\Q$root_dir//; # cut the leading path
55              
56 1         2 my $content;
57 1         16 $self->_engine->out_method( \$content );
58 1         30925 $self->_engine->exec($template, %$tokens);
59 1         4333 return $content;
60             }
61              
62             1;
63              
64             __END__