[32205] | 1 | package Mojolicious::Plugin::EPLRenderer;
|
---|
| 2 | use Mojo::Base 'Mojolicious::Plugin';
|
---|
| 3 |
|
---|
| 4 | use Mojo::Template;
|
---|
| 5 | use Mojo::Util qw(encode md5_sum);
|
---|
| 6 |
|
---|
| 7 | sub register {
|
---|
| 8 | my ($self, $app) = @_;
|
---|
| 9 | $app->renderer->add_handler(
|
---|
| 10 | epl => sub { _render(@_, Mojo::Template->new, $_[1]) });
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | sub _render {
|
---|
| 14 | my ($renderer, $c, $output, $options, $mt, @args) = @_;
|
---|
| 15 |
|
---|
| 16 | # Cached
|
---|
| 17 | if ($mt->compiled) {
|
---|
| 18 | $c->app->log->debug("Rendering cached @{[$mt->name]}");
|
---|
| 19 | $$output = $mt->process(@args);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | # Not cached
|
---|
| 23 | else {
|
---|
| 24 | my $inline = $options->{inline};
|
---|
| 25 | my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef;
|
---|
| 26 | return unless defined($name //= $renderer->template_name($options));
|
---|
| 27 |
|
---|
| 28 | # Inline
|
---|
| 29 | if (defined $inline) {
|
---|
| 30 | $c->app->log->debug(qq{Rendering inline template "$name"});
|
---|
| 31 | $$output = $mt->name(qq{inline template "$name"})->render($inline, @args);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | # File
|
---|
| 35 | else {
|
---|
| 36 | if (my $encoding = $renderer->encoding) { $mt->encoding($encoding) }
|
---|
| 37 |
|
---|
| 38 | # Try template
|
---|
| 39 | if (defined(my $path = $renderer->template_path($options))) {
|
---|
| 40 | $c->app->log->debug(qq{Rendering template "$name"});
|
---|
| 41 | $$output = $mt->name(qq{template "$name"})->render_file($path, @args);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | # Try DATA section
|
---|
| 45 | elsif (defined(my $d = $renderer->get_data_template($options))) {
|
---|
| 46 | $c->app->log->debug(qq{Rendering template "$name" from DATA section});
|
---|
| 47 | $$output = $mt->name(qq{template "$name" from DATA section})
|
---|
| 48 | ->render($d, @args);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | # No template
|
---|
| 52 | else { $c->app->log->debug(qq{Template "$name" not found}) }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | # Exception
|
---|
| 57 | die $$output if ref $$output;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | 1;
|
---|
| 61 |
|
---|
| 62 | =encoding utf8
|
---|
| 63 |
|
---|
| 64 | =head1 NAME
|
---|
| 65 |
|
---|
| 66 | Mojolicious::Plugin::EPLRenderer - Embedded Perl Lite renderer plugin
|
---|
| 67 |
|
---|
| 68 | =head1 SYNOPSIS
|
---|
| 69 |
|
---|
| 70 | # Mojolicious
|
---|
| 71 | $app->plugin('EPLRenderer');
|
---|
| 72 |
|
---|
| 73 | # Mojolicious::Lite
|
---|
| 74 | plugin 'EPLRenderer';
|
---|
| 75 |
|
---|
| 76 | =head1 DESCRIPTION
|
---|
| 77 |
|
---|
| 78 | L<Mojolicious::Plugin::EPLRenderer> is a renderer for C<epl> templates, which
|
---|
| 79 | are pretty much just raw L<Mojo::Template>.
|
---|
| 80 |
|
---|
| 81 | This is a core plugin, that means it is always enabled and its code a good
|
---|
| 82 | example for learning to build new plugins, you're welcome to fork it.
|
---|
| 83 |
|
---|
| 84 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
|
---|
| 85 | by default.
|
---|
| 86 |
|
---|
| 87 | =head1 METHODS
|
---|
| 88 |
|
---|
| 89 | L<Mojolicious::Plugin::EPLRenderer> inherits all methods from
|
---|
| 90 | L<Mojolicious::Plugin> and implements the following new ones.
|
---|
| 91 |
|
---|
| 92 | =head2 register
|
---|
| 93 |
|
---|
| 94 | $plugin->register(Mojolicious->new);
|
---|
| 95 |
|
---|
| 96 | Register renderer in L<Mojolicious> application.
|
---|
| 97 |
|
---|
| 98 | =head1 SEE ALSO
|
---|
| 99 |
|
---|
| 100 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
| 101 |
|
---|
| 102 | =cut
|
---|