| 1 | package MojoX::Renderer::IncludeLater; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.02'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $counter = 0; | |||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub register { | ||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 10 | 0 | my ($self, $app) = @_; | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | $app->helper(include_later => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 13 | 0 | my ($self, $template) = (shift, shift); | |||||||||
| 14 | 0 | ▶ | my $args = scalar @_ % 2 == 0 ? { @_ } : shift; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | my $key = "\0IL\0" . ++$counter . "\0"; | |||||||||
| 16 | |||||||||||
| 17 | 0 | $self->stash->{'mojo.x.include_later'}->{$key} = [ $template, $args ]; | |||||||||
| 18 | 0 | return $key; | |||||||||
| 19 | 0 | }); | |||||||||
| 20 | |||||||||||
| 21 | 0 | ▶ | $app->hook(after_render => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 22 | 0 | my ($self, $output, $format) = @_; | |||||||||
| 23 | 0 | ▶ | while(my ($id) = $$output =~ /(\0IL\0\d+\0)/m) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my ($template, $args) = @{$self->stash->{'mojo.x.include_later'}->{$id}}; | |||||||||
| 25 | 0 | ▶ | my ($op, $format) = $app->renderer->render($self, { partial => 1, template => $template, $args ? %$args : () }); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | ▶ | warn "Error rendering include_later" if !$op; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | $id = quotemeta $id; | |||||||||
| 28 | 0 | $$output =~ s/$id/$op/m; | |||||||||
| 29 | } | ||||||||||
| 30 | 0 | }); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 1 | 1; | |||||||||
| 34 | |||||||||||
| 35 | =encoding utf8 | ||||||||||
| 36 | |||||||||||
| 37 | =head1 NAME | ||||||||||
| 38 | |||||||||||
| 39 | MojoX::Renderer::IncludeLater - A post processor to defer partial template rendering | ||||||||||
| 40 | |||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||
| 42 | |||||||||||
| 43 | L<MojoX::Renderer::IncludeLater> is a L<Mojolicious> plugin which adds support for | ||||||||||
| 44 | deferring rendering of partial templates until the parent template rendering is complete. | ||||||||||
| 45 | |||||||||||
| 46 | For example, this makes it possible to build up data during rendering (e.g. which | ||||||||||
| 47 | input fields are rendered) and then use that data to render an earlier part of a template. | ||||||||||
| 48 | |||||||||||
| 49 | This should work with any L<Mojolicious> renderer, including L<Mojolicious::Renderer> and | ||||||||||
| 50 | L<Mojolicious::Renderer::Xslate>. | ||||||||||
| 51 | |||||||||||
| 52 | =head1 SYNOPSIS | ||||||||||
| 53 | |||||||||||
| 54 | Example 'test' template: | ||||||||||
| 55 | |||||||||||
| 56 | % stash('my_var') // 'my_var has not been set' | ||||||||||
| 57 | |||||||||||
| 58 | Example page template: | ||||||||||
| 59 | |||||||||||
| 60 | <h3>Include later</h3> | ||||||||||
| 61 | <p>Include a template immediately</p> | ||||||||||
| 62 | % include "test" # will render 'my_var has not been set' | ||||||||||
| 63 | |||||||||||
| 64 | <p>Include a template later</p> | ||||||||||
| 65 | % include_later "test" # will render 'foo' | ||||||||||
| 66 | |||||||||||
| 67 | <p>Set a value the included template expects</p> | ||||||||||
| 68 | % stash('test' => 'foo') | ||||||||||
| 69 | |||||||||||
| 70 | Which will generate the following output: | ||||||||||
| 71 | |||||||||||
| 72 | <h3>Include later</h3> | ||||||||||
| 73 | <p>Include a template immediately</p> | ||||||||||
| 74 | my_var has not been set | ||||||||||
| 75 | |||||||||||
| 76 | <p>Include a template later</p> | ||||||||||
| 77 | foo | ||||||||||
| 78 | |||||||||||
| 79 | <p>Set a value the included template expects</p> | ||||||||||
| 80 | |||||||||||
| 81 | =head1 HELPERS | ||||||||||
| 82 | |||||||||||
| 83 | This plugin creates the following L<Mojolicious> helpers: | ||||||||||
| 84 | |||||||||||
| 85 | =head2 include_later | ||||||||||
| 86 | |||||||||||
| 87 | Is identical to C<include> but template inclusion happens after | ||||||||||
| 88 | the rest of the template has been rendered. | ||||||||||
| 89 | |||||||||||
| 90 | =head1 HOOKS | ||||||||||
| 91 | |||||||||||
| 92 | This plugin hooks into C<after_render> to perform deferred template inclusion. | ||||||||||
| 93 | |||||||||||
| 94 | =head1 SEE ALSO | ||||||||||
| 95 | |||||||||||
| 96 | L<Mojolicious> | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||