Skip to content

Commit

Permalink
improve renderer not to require a return value from handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 21, 2015
1 parent d040ec4 commit c7a22d7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.33 2015-11-20
6.33 2015-11-22
- Updated IO::Socket::IP requirement to 0.37 for certain bug fixes.
- Improved renderer not to require a return value from handlers.
- Improved helper method in Mojolicious to replace already existing helpers
silently.

Expand Down
11 changes: 3 additions & 8 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -1399,14 +1399,11 @@ called.
# This part is up to you and your template system :)
...

# Just die if an error occurs
die 'Something went wrong with the template';

# Or pass the rendered result back to the renderer
# Pass the rendered result back to the renderer
$$output = 'Hello World!';

# And return true if something has been rendered or false otherwise
return 1;
# Or just die if an error occurs
die 'Something went wrong with the template';
});
}

Expand Down Expand Up @@ -1445,8 +1442,6 @@ generating bytes instead.

# Encode data from stash value
$$output = nfreeze delete $c->stash->{storable};

return 1;
});

# Set "handler" value automatically if "storable" value is set already
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Plugin/EPLRenderer.pm
Expand Up @@ -21,7 +21,7 @@ sub _epl {
else {
my $inline = $options->{inline};
my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef;
return undef unless defined($name //= $renderer->template_name($options));
return unless defined($name //= $renderer->template_name($options));

# Inline
if (defined $inline) {
Expand All @@ -47,12 +47,12 @@ sub _epl {
}

# No template
else { $log->debug(qq{Template "$name" not found}) and return undef }
else { $log->debug(qq{Template "$name" not found}) }
}
}

# Exception or success
return ref $$output ? die $$output : 1;
# Exception
die $$output if ref $$output;
}

1;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -20,7 +20,7 @@ sub register {
my ($renderer, $c, $output, $options) = @_;
my $name = $options->{inline} // $renderer->template_name($options);
return undef unless defined $name;
return unless defined $name;
my @keys = sort grep {/^\w+$/} keys %{$c->stash};
my $key = md5_sum encode 'UTF-8', join(',', $name, @keys);

Expand All @@ -47,7 +47,7 @@ sub register {
local *{"${ns}::_C"} = sub {$c};

# Render with "epl" handler
return $renderer->handlers->{epl}($renderer, $c, $output, $options);
$renderer->handlers->{epl}($renderer, $c, $output, $options);
}
);
}
Expand Down
8 changes: 2 additions & 6 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -16,12 +16,8 @@ sub register {
$app->renderer->add_handler(
$conf->{name} || 'pod' => sub {
my ($renderer, $c, $output, $options) = @_;

# Preprocess and render
my $handler = $renderer->handlers->{$preprocess};
return undef unless $handler->($renderer, $c, $output, $options);
$$output = _pod_to_html($$output);
return 1;
$renderer->handlers->{$preprocess}($renderer, $c, $output, $options);
$$output = _pod_to_html($$output) if defined $$output;
}
);

Expand Down
14 changes: 6 additions & 8 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -138,7 +138,8 @@ sub render {
while ((my $next = _next($stash)) && !defined $inline) {
@$options{qw(handler template)} = ($stash->{handler}, $next);
$options->{format} = $stash->{format} || $self->default_format;
$self->_render_template($c, \$output, $options);
my $layout;
$output = $layout if $self->_render_template($c, \$layout, $options);
$content->{content} = $output
if $content->{content} !~ /\S/ && $output =~ /\S/;
}
Expand Down Expand Up @@ -234,16 +235,13 @@ sub _next {
sub _render_template {
my ($self, $c, $output, $options) = @_;

# Find handler and render
my $handler = $options->{handler} ||= $self->template_handler($options);
return undef unless $handler;
if (my $renderer = $self->handlers->{$handler}) {
return 1 if $renderer->($self, $c, $output, $options);
}
$c->app->log->error(qq{No handler for "$handler" available}) and return undef
unless my $renderer = $self->handlers->{$handler};

# No handler
else { $c->app->log->error(qq{No handler for "$handler" available}) }
return undef;
$renderer->($self, $c, $output, $options);
return 1 if defined $$output;

This comment has been minimized.

Copy link
@KES777

KES777 Feb 28, 2016

Contributor

PBP 198.

The return value still maybe true if $$output is not defined. This depend on previous expression result

Improved renderer not to require a return value from handlers.

if return value is not required you may remove this line at all

}

1;
Expand Down
3 changes: 2 additions & 1 deletion t/mojolicious/renderer.t
Expand Up @@ -15,7 +15,8 @@ my $renderer = $c->app->renderer->default_format('test');
$renderer->add_handler(
debug => sub {
my ($renderer, $c, $output) = @_;
$$output .= 'Hello Mojo!';
my $content = $c->content // '';
$$output = "Hello Mojo!$content";
}
);
$c->stash->{template} = 'something';
Expand Down

0 comments on commit c7a22d7

Please sign in to comment.