package MojoX::Renderer::IncludeLater; use Mojo::Base 'Mojolicious::Plugin'; our $VERSION = '0.01'; our $counter = 0; sub register { my ($self, $app) = @_; $app->helper(include_later => sub { my ($self, $template, $args) = @_; my $key = "\0IL\0" . ++$counter . "\0"; $self->stash->{'mojo.x.include_later'}->{$key} = [ $template, $args ]; return $key; }); $app->hook(after_render => sub { my ($self, $output, $format) = @_; while(my ($id) = $$output =~ /(\0IL\0\d+\0)/m) { my ($template, $args) = @{$self->stash->{'mojo.x.include_later'}->{$id}}; my ($op, $format) = $app->renderer->render($self, { partial => 1, template => $template, %$args }); warn "Error rendering include_later" if !$op; $id = quotemeta $id; $$output =~ s/$id/$op/m; } }); } 1; =encoding utf8 =head1 NAME MojoX::Renderer::IncludeLater - A post processor to defer partial template rendering =head1 DESCRIPTION L is a L plugin which adds support for deferring rendering of partial templates until the parent template rendering is complete. For example, this makes it possible to build up data during rendering (e.g. which input fields are rendered) and then use that data to render an earlier part of a template. This should work with any L renderer, including L and L. =head1 SYNOPSIS Example 'test' template: % stash('my_var') // 'my_var has not been set' Example page template:

Include later

Include a template immediately

% include "test" # will render 'my_var has not been set'

Include a template later

% include_later "test" # will render 'foo'

Set a value the included template expects

% stash('test' => 'foo') Which will generate the following output:

Include later

Include a template immediately

my_var has not been set

Include a template later

foo

Set a value the included template expects

=head1 HELPERS This plugin creates the following L helpers: =head2 include_later Is identical to C but template inclusion happens after the rest of the template has been rendered. =head1 HOOKS This plugin hooks into C to perform deferred template inclusion. =head1 SEE ALSO L =cut