File Coverage

blib/lib/Dancer2/Template/Haml.pm
Criterion Covered Total %
statement 47 47 100.0
branch 7 8 87.5
condition n/a
subroutine 14 14 100.0
pod 0 4 0.0
total 68 73 93.1


line stmt bran cond sub pod time code
1             package Dancer2::Template::Haml;
2 3     3   3915 use 5.008005;
  3         12  
  3         136  
3 3     3   18 use strict;
  3         6  
  3         122  
4 3     3   18 use warnings FATAL => 'all';
  3         15  
  3         543  
5 3     3   4196 use utf8;
  3         36  
  3         21  
6              
7 3     3   1178 use Moo;
  3         26178  
  3         24  
8              
9 3     3   4794 use Dancer2::Core::Types 'InstanceOf';
  3         43057  
  3         593  
10 3     3   1303 use Dancer2::FileUtils 'path';
  3         818  
  3         261  
11              
12 3     3   17 use Carp qw/croak/;
  3         7  
  3         135  
13              
14 3     3   3385 use Text::Haml;
  3         74045  
  3         1485  
15              
16             our $VERSION = 0.04; # VERSION
17             # ABSTRACT: Text::Haml template engine wrapper for Dancer2
18              
19             with 'Dancer2::Core::Role::Template';
20              
21             has '+default_tmpl_ext' => ( default => sub { 'haml' } );
22             has '+engine' => ( isa => InstanceOf['Text::Haml']);
23              
24             sub view_pathname {
25 4     4 0 238194 my ($self, $view) = @_;
26              
27 4         30 $view = $self->_template_name($view);
28              
29 4 100       110 return (ref $self->config->{path} eq 'HASH') # virtual path
30             ? $view
31             : path($self->views, $view);
32              
33             }
34              
35             sub layout_pathname {
36 2     2 0 7 my ($self, $layout) = @_;
37              
38 2         7 $layout = $self->_template_name($layout);
39              
40 2 100       26 return (ref $self->config->{path} eq 'HASH') # virtual path
41             ? path('layouts', $layout)
42             : path($self->views, 'layouts', $layout);
43             }
44              
45             sub render_layout {
46 2     2 0 612 my ($self, $layout, $tokens, $content) = @_;
47            
48 2         10 $layout = $self->layout_pathname($layout);
49              
50 2         222 $self->engine->escape_html(0);
51            
52             # FIXME: not sure if I can "just call render"
53 2         39 $self->render( $layout, { %$tokens, content => $content } );
54             }
55            
56             sub _build_engine {
57 2     2   1136 my $self = shift;
58              
59 2         7 my %haml_args = %{ $self->config };
  2         9  
60              
61             #$haml_args{path} //= [$haml_args{location}]; # for Perl v5.10
62 2 100       75 $haml_args{path} = defined $haml_args{path}
63             ? $haml_args{path}
64             : [$haml_args{location}];
65              
66 2         23 return Text::Haml->new(%haml_args);
67             }
68              
69             sub render {
70 6     6 0 4543 my ($self, $template, $vars) = @_;
71              
72 6         106 my $haml = $self->engine;
73 6 50       442 my $content = $haml->render_file($template, %$vars)
74             or croak $haml->error;
75              
76             # In the method layout set escape_html in 0 to insert the contents of a page
77             # For all other cases set escape_html 1
78 6         16131 $haml->escape_html(1);
79              
80 6         48 return $content;
81             }
82              
83             1;
84             __END__