1 | package Mojolicious::Plugin::EPRenderer;
|
---|
2 | use Mojo::Base 'Mojolicious::Plugin::EPLRenderer';
|
---|
3 |
|
---|
4 | use Mojo::Template;
|
---|
5 | use Mojo::Util qw(encode md5_sum monkey_patch);
|
---|
6 |
|
---|
7 | sub DESTROY { Mojo::Util::_teardown(shift->{namespace}) }
|
---|
8 |
|
---|
9 | sub register {
|
---|
10 | my ($self, $app, $conf) = @_;
|
---|
11 |
|
---|
12 | # Auto escape by default to prevent XSS attacks
|
---|
13 | my $ep = {auto_escape => 1, %{$conf->{template} || {}}, vars => 1};
|
---|
14 | my $ns = $self->{namespace} = $ep->{namespace}
|
---|
15 | //= 'Mojo::Template::Sandbox::' . md5_sum "$self";
|
---|
16 |
|
---|
17 | # Make "$self" and "$c" available in templates
|
---|
18 | $ep->{prepend} = 'my $self = my $c = _C;' . ($ep->{prepend} // '');
|
---|
19 |
|
---|
20 | # Add "ep" handler and make it the default
|
---|
21 | $app->renderer->default_handler('ep')->add_handler(
|
---|
22 | $conf->{name} || 'ep' => sub {
|
---|
23 | my ($renderer, $c, $output, $options) = @_;
|
---|
24 |
|
---|
25 | my $name = $options->{inline} // $renderer->template_name($options);
|
---|
26 | return unless defined $name;
|
---|
27 | my $key = md5_sum encode 'UTF-8', $name;
|
---|
28 |
|
---|
29 | my $cache = $renderer->cache;
|
---|
30 | my $mt = $cache->get($key);
|
---|
31 | $cache->set($key => $mt = Mojo::Template->new($ep)) unless $mt;
|
---|
32 |
|
---|
33 | # Export helpers only once
|
---|
34 | ++$self->{helpers} and _helpers($ns, $renderer->helpers)
|
---|
35 | unless $self->{helpers};
|
---|
36 |
|
---|
37 | # Make current controller available and render with "epl" handler
|
---|
38 | no strict 'refs';
|
---|
39 | no warnings 'redefine';
|
---|
40 | local *{"${ns}::_C"} = sub {$c};
|
---|
41 | Mojolicious::Plugin::EPLRenderer::_render($renderer, $c, $output,
|
---|
42 | $options, $mt, $c->stash);
|
---|
43 | }
|
---|
44 | );
|
---|
45 | }
|
---|
46 |
|
---|
47 | sub _helpers {
|
---|
48 | my ($class, $helpers) = @_;
|
---|
49 | for my $method (grep {/^\w+$/} keys %$helpers) {
|
---|
50 | my $sub = $helpers->{$method};
|
---|
51 | monkey_patch $class, $method, sub { $class->_C->$sub(@_) };
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | 1;
|
---|
56 |
|
---|
57 | =encoding utf8
|
---|
58 |
|
---|
59 | =head1 NAME
|
---|
60 |
|
---|
61 | Mojolicious::Plugin::EPRenderer - Embedded Perl renderer plugin
|
---|
62 |
|
---|
63 | =head1 SYNOPSIS
|
---|
64 |
|
---|
65 | # Mojolicious
|
---|
66 | $app->plugin('EPRenderer');
|
---|
67 | $app->plugin(EPRenderer => {name => 'foo'});
|
---|
68 | $app->plugin(EPRenderer => {name => 'bar', template => {line_start => '.'}});
|
---|
69 |
|
---|
70 | # Mojolicious::Lite
|
---|
71 | plugin 'EPRenderer';
|
---|
72 | plugin EPRenderer => {name => 'foo'};
|
---|
73 | plugin EPRenderer => {name => 'bar', template => {line_start => '.'}};
|
---|
74 |
|
---|
75 | =head1 DESCRIPTION
|
---|
76 |
|
---|
77 | L<Mojolicious::Plugin::EPRenderer> is a renderer for Embedded Perl templates.
|
---|
78 | For more information see L<Mojolicious::Guides::Rendering/"Embedded Perl">.
|
---|
79 |
|
---|
80 | This is a core plugin, that means it is always enabled and its code a good
|
---|
81 | example for learning to build new plugins, you're welcome to fork it.
|
---|
82 |
|
---|
83 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
|
---|
84 | by default.
|
---|
85 |
|
---|
86 | =head1 OPTIONS
|
---|
87 |
|
---|
88 | L<Mojolicious::Plugin::EPRenderer> supports the following options.
|
---|
89 |
|
---|
90 | =head2 name
|
---|
91 |
|
---|
92 | # Mojolicious::Lite
|
---|
93 | plugin EPRenderer => {name => 'foo'};
|
---|
94 |
|
---|
95 | Handler name, defaults to C<ep>.
|
---|
96 |
|
---|
97 | =head2 template
|
---|
98 |
|
---|
99 | # Mojolicious::Lite
|
---|
100 | plugin EPRenderer => {template => {line_start => '.'}};
|
---|
101 |
|
---|
102 | Attribute values passed to L<Mojo::Template> object used to render templates.
|
---|
103 |
|
---|
104 | =head1 METHODS
|
---|
105 |
|
---|
106 | L<Mojolicious::Plugin::EPRenderer> inherits all methods from
|
---|
107 | L<Mojolicious::Plugin::EPLRenderer> and implements the following new ones.
|
---|
108 |
|
---|
109 | =head2 register
|
---|
110 |
|
---|
111 | $plugin->register(Mojolicious->new);
|
---|
112 | $plugin->register(Mojolicious->new, {name => 'foo'});
|
---|
113 |
|
---|
114 | Register renderer in L<Mojolicious> application.
|
---|
115 |
|
---|
116 | =head1 SEE ALSO
|
---|
117 |
|
---|
118 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
119 |
|
---|
120 | =cut
|
---|