| File: | lib/Mojolicious/Plugin/ToolkitRenderer.pm |
| Coverage: | 61.8% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package Mojolicious::Plugin::ToolkitRenderer; | ||||||
| 2 | # ABSTRACT: Template Toolkit Renderer Mojolicious Plugin | ||||||
| 3 | |||||||
| 4 | 1 1 1 | 1061 9 49 | use strict; | ||||
| 5 | 1 1 1 | 10 6 44 | use warnings; | ||||
| 6 | |||||||
| 7 | 1 1 1 | 9 5 12 | use Mojo::Base 'Mojolicious::Plugin'; | ||||
| 8 | 1 1 1 | 196 6 11 | use Mojo::Exception; | ||||
| 9 | 1 1 1 | 676 26539 329 | use Template (); | ||||
| 10 | |||||||
| 11 | # VERSION | ||||||
| 12 | |||||||
| 13 | sub register { | ||||||
| 14 | 1 | 1 | 51 | my ( $self, $app, $settings ) = @_; | |||
| 15 | |||||||
| 16 | 1 | 18 | my $template = Template->new( $settings->{'config'} || { | ||||
| 17 | 'RELATIVE' => 1, | ||||||
| 18 | 'EVAL_PERL' => 0, | ||||||
| 19 | } ); | ||||||
| 20 | |||||||
| 21 | 1 | 31814 | $settings->{'context'}->( $template->context ) if ( $settings->{'context'} ); | ||||
| 22 | |||||||
| 23 | $app->renderer->add_handler( 'tt' => sub { | ||||||
| 24 | 1 | 149 | my ( $renderer, $controller, $output, $options ) = @_; | ||||
| 25 | 1 | 10 | my $inline = $settings->{'settings'}{'inline_template'} || 'inline'; | ||||
| 26 | |||||||
| 27 | $template->process( | ||||||
| 28 | ( ( $options->{$inline} ) ? \$options->{$inline} : $renderer->template_name($options) ), | ||||||
| 29 | { | ||||||
| 30 | 'content' => $controller->content, | ||||||
| 31 | 1 | 81 | %{ $controller->stash }, | ||||
| 32 | ( $settings->{'settings'}{'controller'} || 'c' ) => $controller, | ||||||
| 33 | }, | ||||||
| 34 | $output, | ||||||
| 35 | 1 | 16 | ) || do { | ||||
| 36 | 0 | 0 | $app->log->error( $template->error ); | ||||
| 37 | |||||||
| 38 | 0 | 0 | if ( | ||||
| 39 | $app->mode ne 'development' and | ||||||
| 40 | ref( $settings->{'settings'}{'error_handler'} ) eq 'CODE' | ||||||
| 41 | ) { | ||||||
| 42 | 0 | 0 | $settings->{'settings'}{'error_handler'}->( $controller, $renderer, $app ); | ||||
| 43 | } | ||||||
| 44 | else { | ||||||
| 45 | 0 | 0 | my $default_handler = $renderer->default_handler; | ||||
| 46 | 0 | 0 | $renderer->default_handler('ep'); | ||||
| 47 | |||||||
| 48 | 0 | 0 | $controller->reply->exception( | ||||
| 49 | Mojo::Exception->new( __PACKAGE__ . ' - ' . $template->error || '' ) | ||||||
| 50 | ); | ||||||
| 51 | |||||||
| 52 | 0 | 0 | $renderer->default_handler($default_handler); | ||||
| 53 | |||||||
| 54 | 0 | 0 | $controller->rendered( | ||||
| 55 | ( $template->error and $template->error =~ /not found/ ) ? 404 : 500 | ||||||
| 56 | ); | ||||||
| 57 | } | ||||||
| 58 | }; | ||||||
| 59 | |||||||
| 60 | 1 | 132 | return $$output; | ||||
| 61 | 1 | 131 | } ); | ||||
| 62 | |||||||
| 63 | $app->helper( | ||||||
| 64 | 'render_tt' => sub { | ||||||
| 65 | 1 | 201 | shift->render( 'handler' => 'tt', @_ ); | ||||
| 66 | } | ||||||
| 67 | 1 | 121 | ); | ||||
| 68 | |||||||
| 69 | 1 | 41 | return; | ||||
| 70 | } | ||||||
| 71 | |||||||
| 72 | 1; | ||||||