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   346395 use v5.8;
  1         9  
4 1     1   5 use strict;
  1         2  
  1         23  
5 1     1   4 use warnings FATAL => 'all';
  1         2  
  1         46  
6 1     1   560 use utf8;
  1         14  
  1         6  
7              
8 1     1   31 use Moo;
  1         3  
  1         6  
9              
10 1     1   328 use Carp qw(croak);
  1         2  
  1         45  
11 1     1   6 use Dancer2::Core::Types qw(InstanceOf);
  1         2  
  1         7  
12 1     1   3448 use Text::Xslate;
  1         12065  
  1         58  
13 1     1   8 use File::Spec::Functions qw(abs2rel file_name_is_absolute);
  1         2  
  1         355  
14              
15             our $VERSION = 'v0.2.0'; # 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   12 my ($self) = @_;
29              
30 1         3 my %config = %{ $self->config };
  1         19  
31              
32             # Dancer2 injects a couple options without asking; Text::Xslate protests:
33 1         2 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 119658 my ($self, $tmpl, $vars) = @_;
45              
46 2         38 my $xslate = $self->engine;
47 2         332 my $content = eval {
48 2 50       8 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       8 : $tmpl;
55 2         221 $xslate->render($rel_path, $vars);
56             }
57             };
58              
59 2 50       70312 $@ and croak $@;
60              
61 2         10 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             If you want to use cascading templates to manage page layouts
89             make sure you disable Dancer2 layouts.
90              
91             In C<config.yaml>, comment the C<layout> keyword:
92              
93             # The default layout to use for your application (located in
94             # views/layouts/main.tt)
95             # layout: "main"
96              
97             If you don't, Dancer2 will render your template and then render
98             the C<layout> template. If your layout template doesn't have the
99             C<content> placeholder only the layout HTML will be returned.
100              
101             =head1 METHODS
102              
103             =over
104              
105             =item render($template, $tokens)
106              
107             Renders a template. C<$template> can be one of:
108              
109             =over 2
110              
111             =item *
112              
113             a string of the path to a template file (*.tx, not *.tt like the core Dancer2
114             template engines)
115              
116             =item *
117              
118             a reference to a string containing prerendered template content
119              
120             =back
121              
122             =back
123              
124             =head1 SEE ALSO
125              
126             =over
127              
128             =item L<Dancer::Template::Xslate>
129              
130             Xslate rendering engine for Dancer 1.
131              
132             =back
133              
134             =head1 AUTHOR
135              
136             Richard Simões C<< <rsimoes AT cpan DOT org> >>
137              
138             =head1 COPYRIGHT AND LICENSE
139              
140             Copyright © 2013 Richard Simões. This module is released under the terms of the
141             B<MIT License> and may be modified and/or redistributed under the same or any
142             compatible license.