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