Newer Older
128 lines | 3.566kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojolicious::Plugin::EPRenderer;
2
use Mojo::Base 'Mojolicious::Plugin';
3

            
4
use Mojo::Template;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
5
use Mojo::Util qw(encode md5_sum monkey_patch);
copy gitweblite soruce code
root authored on 2012-11-23
6

            
7
sub register {
8
  my ($self, $app, $conf) = @_;
9

            
10
  # Auto escape by default to prevent XSS attacks
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
11
  my $template = {auto_escape => 1, %{$conf->{template} || {}}};
copy gitweblite soruce code
root authored on 2012-11-23
12

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
13
  # Add "ep" handler and make it the default
14
  $app->renderer->default_handler('ep')->add_handler(
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
15
    $conf->{name} || 'ep' => sub {
16
      my ($renderer, $c, $output, $options) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
17

            
18
      # Generate name
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
19
      my $path = $options->{inline} || $renderer->template_path($options);
20
      return undef unless defined $path;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
21
      my @keys = sort grep {/^\w+$/} keys %{$c->stash};
22
      my $id = encode 'UTF-8', join(',', $path, @keys);
copy gitweblite soruce code
root authored on 2012-11-23
23
      my $key = $options->{cache} = md5_sum $id;
24

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
25
      # Cache template for "epl" handler
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
26
      my $cache = $renderer->cache;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
27
      my $mt    = $cache->get($key);
28
      unless ($mt) {
29
        $mt = Mojo::Template->new($template);
copy gitweblite soruce code
root authored on 2012-11-23
30

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
31
        # Helpers (only once)
32
        ++$self->{helpers} and _helpers($mt->namespace, $renderer->helpers)
33
          unless $self->{helpers};
copy gitweblite soruce code
root authored on 2012-11-23
34

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
35
        # Stash values (every time)
36
        my $prepend = 'my $self = shift; my $_S = $self->stash;';
37
        $prepend .= " my \$$_ = \$_S->{'$_'};" for @keys;
copy gitweblite soruce code
root authored on 2012-11-23
38

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
39
        $cache->set($key => $mt->prepend($prepend . $mt->prepend));
copy gitweblite soruce code
root authored on 2012-11-23
40
      }
41

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
42
      # Make current controller available
43
      no strict 'refs';
44
      no warnings 'redefine';
45
      local *{"@{[$mt->namespace]}::_C"} = sub {$c};
46

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
47
      # Render with "epl" handler
48
      return $renderer->handlers->{epl}->($renderer, $c, $output, $options);
copy gitweblite soruce code
root authored on 2012-11-23
49
    }
50
  );
51
}
52

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
53
sub _helpers {
54
  my ($namespace, $helpers) = @_;
55
  for my $name (grep {/^\w+$/} keys %$helpers) {
56
    monkey_patch $namespace, $name,
57
      sub { $helpers->{$name}->($namespace->_C, @_) };
58
  }
59
}
60

            
copy gitweblite soruce code
root authored on 2012-11-23
61
1;
62

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
63
=encoding utf8
64

            
copy gitweblite soruce code
root authored on 2012-11-23
65
=head1 NAME
66

            
67
Mojolicious::Plugin::EPRenderer - Embedded Perl renderer plugin
68

            
69
=head1 SYNOPSIS
70

            
71
  # Mojolicious
72
  $self->plugin('EPRenderer');
73
  $self->plugin(EPRenderer => {name => 'foo'});
74
  $self->plugin(EPRenderer => {template => {line_start => '.'}});
75

            
76
  # Mojolicious::Lite
77
  plugin 'EPRenderer';
78
  plugin EPRenderer => {name => 'foo'};
79
  plugin EPRenderer => {template => {line_start => '.'}};
80

            
81
=head1 DESCRIPTION
82

            
83
L<Mojolicious::Plugin::EPRenderer> is a renderer for C<ep> templates.
84

            
85
C<ep> or C<Embedded Perl> is a simple template format where you embed perl
86
code into documents. It is based on L<Mojo::Template>, but extends it with
87
some convenient syntax sugar designed specifically for L<Mojolicious>. It
88
supports L<Mojolicious> template helpers and exposes the stash directly as
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
89
Perl variables.
90

            
91
This is a core plugin, that means it is always enabled and its code a good
92
example for learning to build new plugins, you're welcome to fork it.
copy gitweblite soruce code
root authored on 2012-11-23
93

            
94
=head1 OPTIONS
95

            
96
L<Mojolicious::Plugin::EPRenderer> supports the following options.
97

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
98
=head2 name
copy gitweblite soruce code
root authored on 2012-11-23
99

            
100
  # Mojolicious::Lite
101
  plugin EPRenderer => {name => 'foo'};
102

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
103
Handler name, defaults to C<ep>.
copy gitweblite soruce code
root authored on 2012-11-23
104

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
105
=head2 template
copy gitweblite soruce code
root authored on 2012-11-23
106

            
107
  # Mojolicious::Lite
108
  plugin EPRenderer => {template => {line_start => '.'}};
109

            
110
Attribute values passed to L<Mojo::Template> object used to render templates.
111

            
112
=head1 METHODS
113

            
114
L<Mojolicious::Plugin::EPRenderer> inherits all methods from
115
L<Mojolicious::Plugin> and implements the following new ones.
116

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
117
=head2 register
copy gitweblite soruce code
root authored on 2012-11-23
118

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
119
  $plugin->register(Mojolicious->new);
120
  $plugin->register(Mojolicious->new, {name => 'foo'});
copy gitweblite soruce code
root authored on 2012-11-23
121

            
122
Register renderer in L<Mojolicious> application.
123

            
124
=head1 SEE ALSO
125

            
126
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
127

            
128
=cut