companieshouse / MojoX-Plugin-Hook-BeforeRendered

Add before_renderer hook to Mojolicious. This allows non-blocking activities to take place just before content is rendered to the client.
Other
0 stars 2 forks source link

Can't call render methods from before_rendered hook #1

Open ian-kent opened 10 years ago

ian-kent commented 10 years ago

Not sure if this is a bug or a feature :)

use Mojolicious::Lite;
use Test::Mojo;

plugin 'MojoX::Plugin::Hook::BeforeRendered';
get '/' => sub { shift->render(text => 'foo') };

hook 'before_rendered' => sub {
  my ($next, $c) = @_;
  $c->render(text => 'bar');
};

Test::Mojo->new->get_ok('/');

Throws this error:

t/10_plugin.t ... 1/? Deep recursion on subroutine "Mojolicious::Controller::render" at t/10_plugin.t line 30.
Deep recursion on subroutine "Mojolicious::Plugins::emit_hook" at /usr/local/share/perl/5.14.2/Mojolicious/Controller.pm line 153.
Deep recursion on subroutine "Mojolicious::Plugins::emit_hook" at /usr/local/share/perl/5.14.2/Mojolicious/Controller.pm line 165.
Deep recursion on anonymous subroutine at /usr/local/share/perl/5.14.2/Mojolicious/Controller.pm line 169.
Deep recursion on subroutine "Mojolicious::Plugins::emit_hook" at /home/iankent/github/MojoX-Plugin-Hook-BeforeRendered/lib/MojoX/Plugin/Hook/BeforeRendered.pm line 26.
Deep recursion on anonymous subroutine at /usr/local/share/perl/5.14.2/Mojolicious/Plugins.pm line 10.
necrophonic commented 10 years ago

Makes sense as a "feature"..... The logic holds up that if you call render in before_rendered then it kicks off another "render" chain which in turn has a before_rendered which runs render which..... etc :p