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   203963 use v5.8;
  1         6  
4 1     1   5 use strict;
  1         2  
  1         23  
5 1     1   5 use warnings FATAL => 'all';
  1         2  
  1         39  
6 1     1   939 use utf8;
  1         11  
  1         5  
7              
8 1     1   29 use Moo;
  1         1  
  1         7  
9              
10 1     1   290 use Carp qw(croak);
  1         2  
  1         57  
11 1     1   6 use Dancer2::Core::Types qw(InstanceOf);
  1         1  
  1         162  
12 1     1   999 use Text::Xslate;
  1         10828  
  1         58  
13 1     1   9 use File::Spec::Functions qw(abs2rel file_name_is_absolute);
  1         2  
  1         494  
14              
15             our $VERSION = 'v0.1.1'; # 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   564 my ($self) = @_;
29              
30 1         2 my %config = %{ $self->config };
  1         9  
31              
32             # Dancer2 injects a couple options without asking; Text::Xslate protests:
33 1         3 delete $config{environment};
34 1 50       7 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 154324 my ($self, $tmpl, $vars) = @_;
45              
46 2         35 my $xslate = $self->engine;
47 2         483 my $content = eval {
48 2 50       10 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       11 : $tmpl;
55 2         271 $xslate->render($rel_path, $vars);
56             }
57             };
58              
59 2 50       89427 $@ and croak $@;
60              
61 2         12 return $content;
62             }
63              
64             1;
65             =encoding utf8
66              
67             =head1 NAME
68              
69             Dancer2::Template::Xslate - Text::Xslate template engine for Dancer2
70              
71             =head1 SYNOPSIS
72              
73             C:
74              
75             template: Xslate
76             engines:
77             template:
78             Xslate: { path: "views" }
79              
80             A Dancer 2 application:
81              
82             use Dancer2;
83              
84             get /page/:number => sub {
85             my $page_num = params->{number};
86             template "foo.tx", { page_num => $page_num };
87             };
88              
89             =head1 METHODS
90              
91             =over
92              
93             =item render($template, $tokens)
94              
95             Renders a template. C<$template> can be one of:
96              
97             =over 2
98              
99             =item *
100              
101             a string of the path to a template file (*.tx, not *.tt like the core Dancer2
102             template engines)
103              
104             =item *
105              
106             a reference to a string containing prerendered template content
107              
108             =back
109              
110             =back
111              
112             =head1 SEE ALSO
113              
114             =over
115              
116             =item L
117              
118             Xslate rendering engine for Dancer 1.
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Richard Simões C<< >>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             Copyright © 2013 Richard Simões. This module is released under the terms of the
129             B and may be modified and/or redistributed under the same or any
130             compatible license.