File Coverage

blib/lib/Dancer2/Template/Tiny.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 6 66.6
condition 2 3 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 38 41 92.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Template::Tiny engine for Dancer2
2             $Dancer2::Template::Tiny::VERSION = '0.400000';
3             use Moo;
4 109     109   55283 use Carp qw/croak/;
  109         253  
  109         759  
5 109     109   37976 use Dancer2::Core::Types;
  109         269  
  109         6315  
6 109     109   666 use Dancer2::Template::Implementation::ForkedTiny;
  109         331  
  109         1069  
7 109     109   980707 use Dancer2::FileUtils 'read_file_content';
  109         378  
  109         3674  
8 109     109   770  
  109         238  
  109         19963  
9             with 'Dancer2::Core::Role::Template';
10              
11             has '+engine' => (
12             isa => InstanceOf ['Dancer2::Template::Implementation::ForkedTiny']
13             );
14              
15             Dancer2::Template::Implementation::ForkedTiny->new( %{ $_[0]->config } );
16             }
17 8     8   106  
  8         114  
18             my ( $self, $template, $tokens ) = @_;
19              
20             ( ref $template || -f $template )
21 20     20 1 54 or croak "$template is not a regular file or reference";
22              
23 20 50 66     442 my $template_data =
24             ref $template
25             ? ${$template}
26             : read_file_content($template);
27              
28 20 100       139 my $content;
  1         2  
29              
30             $self->engine->process( \$template_data, $tokens, \$content, )
31 20         42 or die "Could not process template file '$template'";
32              
33 20 50       438 return $content;
34             }
35              
36 20         78 1;
37              
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             Dancer2::Template::Tiny - Template::Tiny engine for Dancer2
46              
47             =head1 VERSION
48              
49             version 0.400000
50              
51             =head1 SYNOPSIS
52              
53             This template engine allows you to use L<Template::Tiny> in L<Dancer2>.
54              
55             L<Template::Tiny> is an implementation of a subset of L<Template::Toolkit> (the
56             major parts) which takes much less memory and is faster. If you're only using
57             the main functions of Template::Toolkit, you could use Template::Tiny. You can
58             also seamlessly move back to Template::Toolkit whenever you want.
59              
60             However, Dancer2 uses a modified version of L<Template::Tiny>, which is L<Dancer2::Template::Implementation::ForkedTiny>. It adds 2 features :
61              
62             =over
63              
64             =item *
65              
66             opening and closing tag are now configurable
67              
68             =item *
69              
70             CodeRefs are evaluated and their results is inserted in the result.
71              
72             =back
73              
74             You can read more on L<Dancer2::Template::Implementation::ForkedTiny>.
75              
76             To use this engine, all you need to configure in your L<Dancer2>'s
77             C<config.yaml>:
78              
79             template: "tiny"
80              
81             Of course, you can also set this B<while> working using C<set>:
82              
83             # code code code
84             set template => 'tiny';
85              
86             Since L<Dancer2> has internal support for a wrapper-like option with the
87             C<layout> configuration option, you can have a L<Template::Toolkit>-like WRAPPER
88             even though L<Template::Tiny> doesn't really support it.
89              
90             =head1 METHODS
91              
92             =head2 render($template, \%tokens)
93              
94             Renders the template. The first arg is a filename for the template file
95             or a reference to a string that contains the template. The second arg
96             is a hashref for the tokens that you wish to pass to
97             L<Template::Toolkit> for rendering.
98              
99             =head1 SEE ALSO
100              
101             L<Dancer2>, L<Dancer2::Core::Role::Template>, L<Template::Tiny>,
102             L<Dancer2::Template::Implementation::ForkedTiny>.
103              
104             =head1 AUTHOR
105              
106             Dancer Core Developers
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2022 by Alexis Sukrieh.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut