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