Debian Codegraph @

lib/Dancer2/Template/Tiny.pm

raw
1 package Dancer2::Template::Tiny;
2 # ABSTRACT: Template::Tiny engine for Dancer2
3 $Dancer2::Template::Tiny::VERSION = '2.2.2';
4 use Moo;
5 use Carp qw/croak/;
6 use Path::Tiny ();
7 use Dancer2::Core::Types;
8 use Template::Tiny;
9
10 with 'Dancer2::Core::Role::Template';
11
12 has '+engine' => (
13 isa => InstanceOf ['Template::Tiny']
14 );
15
16 sub _build_engine {
17 Template::Tiny->new( %{ $_[0]->config } );
18 }
19
20 sub render {
21 my ( $self, $template, $tokens ) = @_;
22
23 ( ref $template || -f $template )
24 or croak "$template is not a regular file or reference";
25
26 my $template_data =
27 ref $template
28 ? ${$template}
29 : Path::Tiny::path($template)->slurp_utf8;
30
31 # Template::Tiny doesn't like empty template files (like .dancer), so
32 # don't try to render them. Return an empty (not undef) value instead.
33 return '' unless $template_data;
34
35 my $content;
36
37 $self->engine->process( \$template_data, $tokens, \$content, )
38 or die "Could not process template file '$template'";
39
40 return $content;
41 }
42
43 1;
44
45 __END__
46
47 =pod
48
49 =encoding UTF-8
50
51 =head1 NAME
52
53 Dancer2::Template::Tiny - Template::Tiny engine for Dancer2
54
55 =head1 VERSION
56
57 version 2.2.2
58
59 =head1 SYNOPSIS
60
61 This template engine allows you to use L<Template::Tiny> in L<Dancer2>.
62
63 L<Template::Tiny> is an implementation of a subset of L<Template::Toolkit> (the
64 major parts) but takes much less memory and is faster. If you're only using
65 the main functions of Template::Toolkit, you could use Template::Tiny. You can
66 also seamlessly move back to Template::Toolkit whenever you want.
67
68 To use this engine, all you need to configure in your L<Dancer2>'s
69 C<config.yaml>:
70
71 template: "tiny"
72
73 Of course, you can also set this B<while> working using C<set>:
74
75 # code code code
76 set template => 'tiny';
77
78 Since L<Dancer2> has internal support for a wrapper-like option with the
79 C<layout> configuration option, you can have a L<Template::Toolkit>-like WRAPPER
80 even though L<Template::Tiny> doesn't really support it.
81
82 =head1 METHODS
83
84 =head2 render($template, \%tokens)
85
86 Renders the template. The first arg is a filename for the template file
87 or a reference to a string that contains the template. The second arg
88 is a hashref for the tokens that you wish to pass to
89 L<Template::Toolkit> for rendering.
90
91 =head1 SEE ALSO
92
93 L<Dancer2>, L<Dancer2::Core::Role::Template>, L<Template::Tiny>.
94
95 =head1 AUTHOR
96
97 Dancer Core Developers
98
99 =head1 COPYRIGHT AND LICENSE
100
101 This software is copyright (c) 2026 by Alexis Sukrieh.
102
103 This is free software; you can redistribute it and/or modify it under
104 the same terms as the Perl 5 programming language system itself.
105
106 =cut