| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::TemplateToolkit; | 
| 2 | 2 |  |  | 2 |  | 16277 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 357 | use Carp; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 134 |  | 
| 5 | 2 |  |  | 2 |  | 14 | use Mojo::Util qw(encode md5_sum); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 93 |  | 
| 6 | 2 |  |  | 2 |  | 1018 | use Template; | 
|  | 2 |  |  |  |  | 39502 |  | 
|  | 2 |  |  |  |  | 75 |  | 
| 7 | 2 |  |  | 2 |  | 934 | use Template::Provider::Mojo; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 1645 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.005'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub register { | 
| 12 | 3 |  |  | 3 | 1 | 4042 | my ($self, $app, $conf) = @_; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 3 |  | 100 |  |  | 22 | my $tt_config = $conf->{template} || {}; | 
| 15 | 3 |  |  |  |  | 19 | $tt_config->{MOJO_RENDERER} = $app->renderer; | 
| 16 | 3 |  |  |  |  | 22 | push @{$tt_config->{LOAD_TEMPLATES}}, Template::Provider::Mojo->new($tt_config); | 
|  | 3 |  |  |  |  | 28 |  | 
| 17 | 3 |  |  |  |  | 123 | my $tt = Template->new($tt_config); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | $app->renderer->add_handler($conf->{name} || 'tt2' => sub { | 
| 20 | 14 |  |  | 14 |  | 61133 | my ($renderer, $c, $output, $options) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 14 |  |  |  |  | 42 | my $inline = $options->{inline}; | 
| 23 | 14 | 100 |  |  |  | 123 | my $name = defined $inline ? md5_sum encode('UTF-8', $inline) : undef; | 
| 24 | 14 | 50 | 66 |  |  | 220 | return unless defined($name //= $renderer->template_name($options)); | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 14 |  |  |  |  | 2276 | my %params; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Helpers | 
| 29 | 14 |  |  |  |  | 31 | foreach my $method (grep { m/^\w+\z/ } keys %{$renderer->helpers}) { | 
|  | 952 |  |  |  |  | 2269 |  | 
|  | 14 |  |  |  |  | 47 |  | 
| 30 | 826 |  |  |  |  | 1779 | my $sub = $renderer->helpers->{$method}; | 
| 31 | 826 |  |  |  |  | 4920 | $params{$method} = sub { carp "Calling helpers directly in templates is deprecated. Use c.$method or h.$method"; $c->$sub(@_) }; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Stash values | 
| 35 | 14 |  |  |  |  | 46 | $params{$_} = $c->stash->{$_} for grep { m/^\w+\z/ } keys %{$c->stash}; | 
|  | 47 |  |  |  |  | 311 |  | 
|  | 14 |  |  |  |  | 50 |  | 
| 36 | 14 |  |  |  |  | 189 | $params{self} = $params{c} = $c; | 
| 37 | 14 |  |  |  |  | 52 | $params{h} = $c->helpers; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # Inline | 
| 40 | 14 | 100 |  |  |  | 6020 | if (defined $inline) { | 
| 41 | 11 |  |  |  |  | 33 | $c->app->log->debug(qq{Rendering inline template "$name"}); | 
| 42 | 11 | 50 |  |  |  | 510 | $tt->process(\$inline, \%params, $output) or die $tt->error, "\n"; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | # File | 
| 46 |  |  |  |  |  |  | else { | 
| 47 |  |  |  |  |  |  | # Try template | 
| 48 | 3 | 100 |  |  |  | 11 | if (defined(my $path = $renderer->template_path($options))) { | 
|  |  | 50 |  |  |  |  |  | 
| 49 | 1 |  |  |  |  | 101 | $c->app->log->debug(qq{Rendering template "$name"}); | 
| 50 | 1 | 50 |  |  |  | 41 | $tt->process($name, \%params, $output) or die $tt->error, "\n"; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # Try DATA section | 
| 54 |  |  |  |  |  |  | elsif (defined(my $d = $renderer->get_data_template($options))) { | 
| 55 | 2 |  |  |  |  | 308 | $c->app->log->debug(qq{Rendering template "$name" from DATA section}); | 
| 56 | 2 | 50 |  |  |  | 80 | $tt->process(\$d, \%params, $output) or die $tt->error, "\n"; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # No template | 
| 60 | 0 |  |  |  |  |  | else { $c->app->log->debug(qq{Template "$name" not found}) } | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 3 |  | 100 |  |  | 45954 | }); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | 1; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head1 NAME | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | Mojolicious::Plugin::TemplateToolkit - Template Toolkit renderer plugin for | 
| 70 |  |  |  |  |  |  | Mojolicious | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # Mojolicious | 
| 75 |  |  |  |  |  |  | $app->plugin('TemplateToolkit'); | 
| 76 |  |  |  |  |  |  | $app->plugin(TemplateToolkit => {name => 'foo'}); | 
| 77 |  |  |  |  |  |  | $app->plugin(TemplateToolkit => {template => {INTERPOLATE => 1}}); | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Mojolicious::Lite | 
| 80 |  |  |  |  |  |  | plugin 'TemplateToolkit'; | 
| 81 |  |  |  |  |  |  | plugin TemplateToolkit => {name => 'foo'}; | 
| 82 |  |  |  |  |  |  | plugin TemplateToolkit => {template => {INTERPOLATE => 1}}); | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # Set as default handler | 
| 85 |  |  |  |  |  |  | $app->renderer->default_handler('tt2'); | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # Render without setting as default handler | 
| 88 |  |  |  |  |  |  | $c->render(template => 'bar', handler => 'tt2'); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | L is a renderer for C or | 
| 93 |  |  |  |  |  |  | C templates. See L and L for | 
| 94 |  |  |  |  |  |  | details on the C format, and L | 
| 95 |  |  |  |  |  |  | for general information on rendering in L. | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Along with template files, inline and data section templates can be rendered in | 
| 98 |  |  |  |  |  |  | the standard Mojolicious fashion. Template files and data sections will be | 
| 99 |  |  |  |  |  |  | retrieved using L via L for | 
| 100 |  |  |  |  |  |  | both direct rendering and directives such as C. This means that | 
| 101 |  |  |  |  |  |  | instead of specifying L, | 
| 102 |  |  |  |  |  |  | you should set L to the appropriate paths. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | $app->renderer->paths(['/path/to/templates']); | 
| 105 |  |  |  |  |  |  | push @{$app->renderer->paths}, '/path/to/more/templates'; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | L stash values will be exposed directly as | 
| 108 |  |  |  |  |  |  | L in the templates, and the current | 
| 109 |  |  |  |  |  |  | controller object will be available as C or C, similar to | 
| 110 |  |  |  |  |  |  | L. Helper methods can be called on the | 
| 111 |  |  |  |  |  |  | L or a more efficient | 
| 112 |  |  |  |  |  |  | L available as C. See | 
| 113 |  |  |  |  |  |  | L and L | 
| 114 |  |  |  |  |  |  | for a list of all built-in helpers. | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Accessing helper methods directly as variables, rather than via the controller | 
| 117 |  |  |  |  |  |  | or proxy object, is deprecated and may be removed in a future release. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | $c->stash(description => 'template engine'); | 
| 120 |  |  |  |  |  |  | $c->stash(engines => [qw(Template::Toolkit Text::Template)]); | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | [% FOREACH engine IN engines %] | 
| 123 |  |  |  |  |  |  | [% engine %] is a [% description %]. | 
| 124 |  |  |  |  |  |  | [% END %] | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | [% h.link_to('Template Toolkit', 'http://www.template-toolkit.org') %] | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | [% c.param('foo') %] | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =head1 OPTIONS | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | L supports the following options. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head2 name | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # Mojolicious::Lite | 
| 138 |  |  |  |  |  |  | plugin TemplateToolkit => {name => 'foo'}; | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | Handler name, defaults to C. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head2 template | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | # Mojolicious::Lite | 
| 145 |  |  |  |  |  |  | plugin TemplateToolkit => {template => {INTERPOLATE => 1}}; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | Configuration values passed to L object used to render templates. | 
| 148 |  |  |  |  |  |  | Note that L will use L | 
| 149 |  |  |  |  |  |  | to find templates, not L | 
| 150 |  |  |  |  |  |  | specified here. | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head1 METHODS | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | L inherits all methods from | 
| 155 |  |  |  |  |  |  | L and implements the following new ones. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =head2 register | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | $plugin->register(Mojolicious->new); | 
| 160 |  |  |  |  |  |  | $plugin->register(Mojolicious->new, {name => 'foo'}); | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | Register renderer in L application. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head1 BUGS | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | Report any issues on the public bugtracker. | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | =head1 AUTHOR | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | Dan Book | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | This software is Copyright (c) 2015 by Dan Book. | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | This is free software, licensed under: | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | L, L, L |