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