TTSite で作った MyApp::View::TT を Catalyst::View::Templated を使うように書き換えてみた

package MyApp::View::TT;

use strict;
use warnings;
use base 'Catalyst::View::Templated';
use Template;

MyApp->config->{'View::TT'} = {
    EVAL_PERL          => 0,
    TEMPLATE_EXTENSION => '.tt2',
    CATALYST_VAR       => 'c',
    INCLUDE_PATH       => [
        MyApp->path_to('root', 'src'),
        MyApp->path_to('root', 'lib'),
    ],
    PRE_PROCESS        => 'config/main',
    WRAPPER            => 'site/wrapper',
    ERROR              => 'error.tt2',
};

sub new {
    my ($class, $c, $args) = @_;
    my $self = $class->next::method($c, $args);

    $self->{engine} = Template->new({
        %{$self->context->config},
        %$args,
    });
    return $self;
}

sub _render {
    my ($self, $template, $stash, $args) = @_;
    my $engine = $self->{engine};

    $engine->process($template, $stash, \my $output,) or die $engine->error;
    return $output;
}

1;

問題なければ、Helper を書く。

使い方はこんな感じ。

$c->view('TT')->template('path_to/baz');
$c->stash->{foo} = 'bar';
$c->detach('View::TT');

テンプレートの指定は省略可能。