Newer Older
104 lines | 2.596kb
add files
Yuki Kimoto authored on 2014-03-26
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 { $_[1]->renderer->add_handler(epl => \&_epl) }
8

            
9
sub _epl {
10
  my ($renderer, $c, $output, $options) = @_;
11

            
12
  # Template
13
  my $inline = $options->{inline};
14
  my $path   = $renderer->template_path($options);
15
  $path = md5_sum encode('UTF-8', $inline) if defined $inline;
16
  return undef unless defined $path;
17

            
18
  # Cached
19
  my $key   = delete $options->{cache} || $path;
20
  my $cache = $renderer->cache;
21
  my $mt    = $cache->get($key);
22
  $mt ||= $cache->set($key => Mojo::Template->new)->get($key);
23
  my $log = $c->app->log;
24
  if ($mt->compiled) {
25
    $log->debug("Rendering cached @{[$mt->name]}.");
26
    $$output = $mt->interpret($c);
27
  }
28

            
29
  # Not cached
30
  else {
31

            
32
    # Inline
33
    if (defined $inline) {
34
      $log->debug('Rendering inline template.');
35
      $$output = $mt->name('inline template')->render($inline, $c);
36
    }
37

            
38
    # File
39
    else {
40
      $mt->encoding($renderer->encoding) if $renderer->encoding;
41
      return undef unless my $t = $renderer->template_name($options);
42

            
43
      # Try template
44
      if (-r $path) {
45
        $log->debug(qq{Rendering template "$t".});
46
        $$output = $mt->name(qq{template "$t"})->render_file($path, $c);
47
      }
48

            
49
      # Try DATA section
50
      elsif (my $d = $renderer->get_data_template($options)) {
51
        $log->debug(qq{Rendering template "$t" from DATA section.});
52
        $$output
53
          = $mt->name(qq{template "$t" from DATA section})->render($d, $c);
54
      }
55

            
56
      # No template
57
      else { $log->debug(qq{Template "$t" not found.}) and return undef }
58
    }
59
  }
60

            
61
  # Exception or success
62
  return ref $$output ? die $$output : 1;
63
}
64

            
65
1;
66

            
67
=encoding utf8
68

            
69
=head1 NAME
70

            
71
Mojolicious::Plugin::EPLRenderer - Embedded Perl Lite renderer plugin
72

            
73
=head1 SYNOPSIS
74

            
75
  # Mojolicious
76
  $self->plugin('EPLRenderer');
77

            
78
  # Mojolicious::Lite
79
  plugin 'EPLRenderer';
80

            
81
=head1 DESCRIPTION
82

            
83
L<Mojolicious::Plugin::EPLRenderer> is a renderer for C<epl> templates. C<epl>
84
templates are pretty much just raw L<Mojo::Template>.
85

            
86
This is a core plugin, that means it is always enabled and its code a good
87
example for learning to build new plugins, you're welcome to fork it.
88

            
89
=head1 METHODS
90

            
91
L<Mojolicious::Plugin::EPLRenderer> inherits all methods from
92
L<Mojolicious::Plugin> and implements the following new ones.
93

            
94
=head2 register
95

            
96
  $plugin->register(Mojolicious->new);
97

            
98
Register renderer in L<Mojolicious> application.
99

            
100
=head1 SEE ALSO
101

            
102
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
103

            
104
=cut