File Coverage

lib/Mojolicious/Plugin/Template/Mustache.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 8 75.0
condition 3 5 60.0
subroutine 4 4 100.0
pod 1 1 100.0
total 36 40 90.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Template::Mustache;
2 1     1   523 use Mojo::Base 'Mojolicious::Plugin';
  1         2  
  1         6  
3              
4 1     1   620 use Template::Mustache;
  1         130855  
  1         271  
5              
6             our $VERSION = '0.03';
7              
8             sub register {
9 1     1 1 70 my (undef, $app, $args) = @_;
10              
11 1   50     9 $args //= {};
12              
13             $app->renderer->add_handler(mustache => sub {
14 3     3   38329 my Mojolicious::Renderer $renderer = shift;
15 3         6 my Mojolicious::Controller $c = shift;
16 3         7 my ($output, $options) = @_;
17              
18 3 100 66     29 if ($options->{inline} and (my $inline_template = $options->{inline})) {
    100          
19 1         11 my $mustache = Template::Mustache->new(
20             template => $inline_template,
21             );
22 1         2035 $$output = $mustache->render($c->stash);
23             }
24             elsif (my $template_name = $renderer->template_path($options)) {
25 1         131 my $mustache = Template::Mustache->new(
26             template_path => $template_name
27             );
28 1         184 $$output = $mustache->render($c->stash);
29             } else {
30 1         1712 my $data_template = $renderer->get_data_template($options);
31 1         57 my $mustache = Template::Mustache->new(
32             template => $data_template
33             );
34 1 50       65 $$output = $mustache->render($c->stash) if $data_template;
35             }
36 3 50       69959 return $$output ? 1 : 0;
37 1         17 });
38              
39 1         29 return 1;
40             }
41              
42             1;
43             __END__
44              
45             =encoding utf8
46              
47             =head1 NAME
48              
49             Mojolicious::Plugin::Template::Mustache - Mojolicious Plugin
50              
51             =head1 SYNOPSIS
52              
53             # Mojolicious
54             $self->plugin('Template::Mustache');
55              
56             # Mojolicious::Lite
57             plugin 'Template::Mustache';
58              
59             get '/inline' => sub {
60             my $c = shift;
61             $c->render(
62             handler => 'mustache',
63             inline => 'Inline hello, {{message}}!',
64             message => 'Mustache',
65             );
66             };
67              
68             =head1 DESCRIPTION
69              
70             L<Mojolicious::Plugin::Template::Mustache> is a L<Mojolicious> plugin.
71              
72             =head1 METHODS
73              
74             L<Mojolicious::Plugin::Template::Mustache> inherits all methods from L<Mojolicious::Plugin>.
75              
76             =head1 SEE ALSO
77              
78             L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>, L<http://mustache.github.io>, L<Template::Mustache>.
79              
80             =head2 AUTHOR
81              
82             Cyrill Novgorodcev E<lt>cynovg@cpan.orgE<gt>
83              
84             =head2 LICENSE
85              
86             Copyright 2017 Cyrill Novgorodcev.
87              
88             This program is free software: you can redistribute it and/or modify
89             it under the terms of the GNU General Public License as published by
90             the Free Software Foundation, either version 3 of the License, or
91             (at your option) any later version.
92              
93             This program is distributed in the hope that it will be useful,
94             but WITHOUT ANY WARRANTY; without even the implied warranty of
95             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96             GNU General Public License for more details.
97              
98             You should have received a copy of the GNU General Public License
99             along with this program. If not, see <http://www.gnu.org/licenses/>.
100              
101             =cut
102