Skip to content

Commit

Permalink
Merge branch 'bugfix/use-send_as-during-template-processing'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason A. Crome committed Jul 13, 2024
2 parents 564f9cb + 626cf66 commit 1f913fd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{$NEXT}}

[ BUG FIXES ]
* None
* GH #1712: Fix use of send_as in templates (Andy Beverley)

[ ENHANCEMENTS ]
* GH #33: Named routes; add uri_for_route keyword (Sawyer X)
Expand Down
13 changes: 11 additions & 2 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,17 @@ sub template {
$self->set_with_return( sub {
$local_response ||= shift;
});
my $content = $template->process( @_ );
# Catch any exceptions that may happen during template processing
my $content = eval { $template->process( @_ ) };
my $eval_result = $@;
$self->set_with_return($old_with_return);
# If there was a previous response set before the exception (or set as
# part of the exception handling), then use that, otherwise throw the
# exception as normal
if ($local_response) {
$self->with_return->($local_response);
} elsif ($eval_result) {
die $eval_result;
}
return $content;
}
Expand Down Expand Up @@ -972,7 +979,9 @@ sub send_as {
$options->{charset} = $self->config->{charset} || 'UTF-8';
my $content = Encode::encode( $options->{charset}, $data );
$options->{content_type} ||= join '/', 'text', lc $type;
$self->send_file( \$content, %$options ); # returns from sub
# Explicit return needed here, as if we are currently rendering a
# template then with_return will not longjump
return $self->send_file( \$content, %$options );
}

# Try and load the serializer class
Expand Down
70 changes: 70 additions & 0 deletions t/issues/gh-1712/gh-1712.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Ref::Util qw<is_coderef>;

# Tests for obscure rendering situations that may occur whilst undertaking
# template processing

{
package MyApp;

use Dancer2;

set template => 'template_toolkit';

hook on_route_exception => sub {
status 200;
send_as(plain => "Some plain text");
};

get '/bork' => sub {
my $bork = sub {
die "I've borked in the template";
};
template 'exec',
{ exec_sub => $bork }
};

get '/plain' => sub {
my $plain = sub {
send_as(plain => "Some plain text");
};
template 'exec',
{ exec_sub => $plain }
};

get '/html' => sub {
my $html = sub {
send_as(html => "<p>HTML text</p>");
};
template 'exec',
{ exec_sub => $html }
};
}

my $app = Dancer2->psgi_app;
ok( is_coderef($app), 'Got app' );

test_psgi $app, sub {
my $cb = shift;

my $res = $cb->(GET '/bork');
is $res->code, 200, 'Correct status code when overriding exception handling';
like $res->content, qr/plain text/, 'Correct content when overriding exception handling';

$res = $cb->(GET '/plain');
is $res->code, 200, 'Correct status when sending as plain during template render';
like $res->content, qr/plain text/, 'Correct content when sending as plain';

$res = $cb->(GET '/html');
is $res->code, 200, 'Correct status when sending as HTML during template render';
like $res->content, qr/HTML text/, 'Correct content when sending as HTML';
};

done_testing();
1 change: 1 addition & 0 deletions t/issues/gh-1712/views/exec.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% IF NOT exec_sub %]foobar[% END %]

0 comments on commit 1f913fd

Please sign in to comment.