package WebCoso::TT; use strict; use warnings; use WebCoso::Common; use Path::Class; use Template; use Log::Log4perl ':easy'; sub new { my ($class,%opts)=@_; $opts{stash}||={ dstUriFor => \&WebCoso::Common::dstUriFor, isLang => \&WebCoso::Common::isLang, }; my $self={%opts}; $self->{template_provider}=Template::Provider->new({ INCLUDE_PATH=> $self->{TMPLPATH}, ABSOLUTE=>1, RELATIVE=>1, }); $self->{template}=Template->new({ LOAD_TEMPLATES=>[$self->{template_provider}], }); $self->{fc}->add_parser( qr{\.tt2?$} => sub { $self->{template}->context->template($_[0]); }); #$}); $self->{expander}=sub { my ($maker,$target,$deps,$matches)=@_; DEBUG("expandTT($target,@$deps,@$matches)"); my $tmpl=$self->{fc}->get($deps->[-1]); my $vars={ path=> $matches->[0], language => $matches->[1], %{$self->{stash}}, }; if (@$deps>1) { INFO("tagging as $deps->[0]"); $vars->{tagged}=$self->{fc}->get($deps->[0]); } push @{$self->{template_provider}->include_path}, file($deps->[-1])->parent; $self->{fc}->put($target, $self->{template}->context->process($tmpl, $vars)); pop @{$self->{template_provider}->include_path}; }; bless $self,$class; } sub expandTT { my ($self)=@_; return $self->{expander} } 1;