| 1 | package Pickles::View::Xslate; | ||
| 2 | use strict; | ||
| 3 | use base qw(Pickles::View); | ||
| 4 | use Text::Xslate; | ||
| 5 | |||
| 6 | my $tx; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | my $self = bless {}, $class; | ||
| 11 | $self; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub render { | ||
| 15 | my( $self, $c ) = @_; | ||
| 16 | my $tx = $self->{xslate}; | ||
| 17 | if (! $tx) { | ||
| 18 | my $config = $self->merge_config( $c ); | ||
| 19 | $tx = $self->{xslate} = Text::Xslate->new( | ||
| 20 | path => [ | ||
| 21 | $c->config->path_to('view'), | ||
| 22 | $c->config->path_to('view', 'inc'), | ||
| 23 | ], | ||
| 24 | %{$config}, | ||
| 25 | $ENV{PLACK_ENV} eq 'production' ? ( cache => 1 ) : (), | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | my $template = $c->stash->{'VIEW_TEMPLATE'}; | ||
| 29 | my $suffix = $tx->{suffix}; | ||
| 30 | unless ( $template =~ /$suffix$/ ) { | ||
| 31 | $template .= $suffix; | ||
| 32 | } | ||
| 33 | my %vars = ( | ||
| 34 | %{$c->stash}, | ||
| 35 | c => $c, | ||
| 36 | ); | ||
| 37 | my $output = $tx->render( $template, \%vars ); | ||
| 38 | return $output; | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |