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 = '0.400001';
4 110     110   58695 use Moo;
  110         284  
  110         893  
5 110     110   43740 use Carp qw/croak/;
  110         322  
  110         6882  
6 110     110   812 use Dancer2::Core::Types;
  110         265  
  110         1204  
7 110     110   1488574 use Dancer2::Template::Implementation::ForkedTiny;
  110         347  
  110         4032  
8 110     110   902 use Dancer2::FileUtils 'read_file_content';
  110         269  
  110         22958  
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 8     8   128 Dancer2::Template::Implementation::ForkedTiny->new( %{ $_[0]->config } );
  8         120  
18             }
19              
20             sub render {
21 20     20 1 63 my ( $self, $template, $tokens ) = @_;
22              
23 20 50 66     445 ( ref $template || -f $template )
24             or croak "$template is not a regular file or reference";
25              
26             my $template_data =
27             ref $template
28 20 100       157 ? ${$template}
  1         3  
29             : read_file_content($template);
30              
31 20         41 my $content;
32              
33 20 50       579 $self->engine->process( \$template_data, $tokens, \$content, )
34             or die "Could not process template file '$template'";
35              
36 20         77 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 0.400001
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