| 1 | package <% $package // $module ~ "::Web::View" %>; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use Carp (); | ||
| 6 | use File::Spec (); | ||
| 7 | |||
| 8 | use File::ShareDir; | ||
| 9 | use Text::Xslate 1.6001; | ||
| 10 | use <% $view_functions_package // ($module ~ "::Web::ViewFunctions") %>; | ||
| 11 | |||
| 12 | # setup view class | ||
| 13 | sub make_instance { | ||
| 14 | my ($class, $context) = @_; | ||
| 15 | Carp::croak("Usage: <% $module %>::Web::View->make_instance(\$context_class)") if @_!=2; | ||
| 16 | |||
| 17 | my $view_conf = $context->config->{'Text::Xslate'} || +{}; | ||
| 18 | unless (exists $view_conf->{path}) { | ||
| 19 | my $tmpl_path = File::Spec->catdir($context->base_dir(), '<% $tmpl_path // "tmpl" %>'); | ||
| 20 | if ( -d $tmpl_path ) { | ||
| 21 | # <% $tmpl_path // 'tmpl' %> | ||
| 22 | $view_conf->{path} = [ $tmpl_path ]; | ||
| 23 | } else { | ||
| 24 | my $share_tmpl_path = eval { File::Spec->catdir(File::ShareDir::dist_dir('<% $dist %>'), '<% $tmpl_path // "tmpl" %>') }; | ||
| 25 | if ($share_tmpl_path) { | ||
| 26 | # This application was installed to system. | ||
| 27 | $view_conf->{path} = [ $share_tmpl_path ]; | ||
| 28 | } else { | ||
| 29 | Carp::croak("Can't find template directory. <% $tmpl_path // 'tmpl' %> Is not available."); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | my $view = Text::Xslate->new(+{ | ||
| 34 | 'syntax' => 'Kolon', | ||
| 35 | 'module' => [ | ||
| 36 | 'Text::Xslate::Bridge::Star', | ||
| 37 | '<% $view_functions_package // ($module ~ "::Web::ViewFunctions") %>', | ||
| 38 | ], | ||
| 39 | 'function' => { | ||
| 40 | }, | ||
| 41 | ($context->debug_mode ? ( warn_handler => sub { | ||
| 42 | Text::Xslate->print( # print method escape html automatically | ||
| 43 | '[[', @_, ']]', | ||
| 44 | ); | ||
| 45 | } ) : () ), | ||
| 46 | %$view_conf | ||
| 47 | }); | ||
| 48 | return $view; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |