File Coverage

blib/lib/Dancer2/Template/Xslate.pm
Criterion Covered Total %
statement 40 43 93.0
branch 4 10 40.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 56 65 86.1


line stmt bran cond sub pod time code
1             package Dancer2::Template::Xslate;
2              
3 1     1   338544 use v5.8;
  1         14  
4 1     1   6 use strict;
  1         2  
  1         26  
5 1     1   4 use warnings FATAL => 'all';
  1         2  
  1         39  
6 1     1   638 use utf8;
  1         15  
  1         8  
7              
8 1     1   33 use Moo;
  1         2  
  1         6  
9              
10 1     1   330 use Carp qw(croak);
  1         2  
  1         60  
11 1     1   8 use Dancer2::Core::Types qw(InstanceOf);
  1         1  
  1         10  
12 1     1   1671 use Text::Xslate;
  1         12471  
  1         56  
13 1     1   7 use File::Spec::Functions qw(abs2rel file_name_is_absolute);
  1         9  
  1         416  
14              
15             our $VERSION = 'v0.1.2'; # VERSION
16             # ABSTRACT: Text::Xslate template engine for Dancer2
17              
18             with 'Dancer2::Core::Role::Template';
19              
20             has '+default_tmpl_ext' => (
21             default => sub { 'tx' }
22             );
23             has '+engine' => (
24             isa => InstanceOf['Text::Xslate']
25             );
26              
27             sub _build_engine {
28 1     1   17 my ($self) = @_;
29              
30 1         3 my %config = %{ $self->config };
  1         23  
31              
32             # Dancer2 injects a couple options without asking; Text::Xslate protests:
33 1         4 delete $config{environment};
34 1 50       5 if ( my $location = delete $config{location} ) {
35 0 0       0 unless (defined $config{path}) {
36 0         0 $config{path} = [$location];
37             }
38             }
39              
40 1         16 return Text::Xslate->new(%config);
41             }
42              
43             sub render {
44 2     2 1 177091 my ($self, $tmpl, $vars) = @_;
45              
46 2         46 my $xslate = $self->engine;
47 2         420 my $content = eval {
48 2 50       9 if ( ref($tmpl) eq 'SCALAR' ) {
49 0         0 $xslate->render_string($$tmpl, $vars)
50             }
51             else {
52             my $rel_path = file_name_is_absolute($tmpl)
53             ? abs2rel($tmpl, $self->config->{location})
54 2 50       15 : $tmpl;
55 2         274 $xslate->render($rel_path, $vars);
56             }
57             };
58              
59 2 50       89459 $@ and croak $@;
60              
61 2         13 return $content;
62             }
63              
64             1;
65              
66              
67             =encoding utf8
68              
69             =head1 NAME
70              
71             Dancer2::Template::Xslate - Text::Xslate template engine for Dancer2
72              
73             =head1 SYNOPSIS
74              
75             C<config.yaml>:
76              
77             template: Xslate
78              
79             A Dancer 2 application:
80              
81             use Dancer2;
82              
83             get '/page/:number' => sub {
84             my $page_num = params->{number};
85             template "foo.tx", { page_num => $page_num };
86             };
87              
88             =head1 METHODS
89              
90             =over
91              
92             =item render($template, $tokens)
93              
94             Renders a template. C<$template> can be one of:
95              
96             =over 2
97              
98             =item *
99              
100             a string of the path to a template file (*.tx, not *.tt like the core Dancer2
101             template engines)
102              
103             =item *
104              
105             a reference to a string containing prerendered template content
106              
107             =back
108              
109             =back
110              
111             =head1 SEE ALSO
112              
113             =over
114              
115             =item L<Dancer::Template::Xslate>
116              
117             Xslate rendering engine for Dancer 1.
118              
119             =back
120              
121             =head1 AUTHOR
122              
123             Richard Simões C<< <rsimoes AT cpan DOT org> >>
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             Copyright © 2013 Richard Simões. This module is released under the terms of the
128             B<MIT License> and may be modified and/or redistributed under the same or any
129             compatible license.