# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*- # # This file is MPL/GPL dual-licensed under the following terms: # # The contents of this file are subject to the Mozilla Public License # Version 1.1 (the "License"); you may not use this file except in # compliance with the License. You may obtain a copy of the License at # http://www.mozilla.org/MPL/ # # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and # limitations under the License. # # The Original Code is PLIF 1.0. # The Initial Developer of the Original Code is Ian Hickson. # # Alternatively, the contents of this file may be used under the terms # of the GNU General Public License Version 2 or later (the "GPL"), in # which case the provisions of the GPL are applicable instead of those # above. If you wish to allow use of your version of this file only # under the terms of the GPL and not to allow others to use your # version of this file under the MPL, indicate your decision by # deleting the provisions above and replace them with the notice and # other provisions required by the GPL. If you do not delete the # provisions above, a recipient may use your version of this file # under either the MPL or the GPL. package PLIF::Service::TemplateToolkit; use strict; use vars qw(@ISA); use PLIF::Service; @ISA = qw(PLIF::Service); 1; sub provides { my $class = shift; my($service) = @_; return ($service eq 'string.expander.TemplateToolkit' or $service eq 'string.expander.TemplateToolkitCompiled' or $class->SUPER::provides($service)); } sub init { my $self = shift; my($app) = @_; $self->SUPER::init(@_); require Template; import Template; # DEPENDENCY eval { package PLIF::Service::TemplateToolkit::Context; require Template::Context; import Template::Context; # DEPENDENCY }; } sub expand { my $self = shift; my($app, $output, $session, $protocol, $string, $data, $type) = @_; local $Template::Config::STASH = 'Template::Stash::Context'; # Don't use Template::Stash::XS as we can't currently get a hash back out of it my $template = Template->new({ 'CONTEXT' => PLIF::Service::TemplateToolkit::Context->new($app, $output, $session, $protocol), }); local $Template::Stash::Context::SCALAR_OPS->{'sprintf'} = sub { sprintf($_[1], $_[0]) }; my $document; if ($type eq 'TemplateToolkitCompiled') { # what we have here is a potential Template::Document # let's try to evaluate it $document = eval $string; $self->assert((not defined($@)), 1, "Error loading compiled template: $@"); } else { # $type eq 'TemplateToolkit' # what we have is a raw string $document = \$string; } # ok, let's try to process it my $result = ''; if (not $template->process($document, $data, \$result)) { $self->error(1, 'Error processing template: '.$template->error()); } return $result; } package PLIF::Service::TemplateToolkit::Context; use strict; use vars qw(@ISA $URI_ESCAPES); @ISA = qw(Template::Context); 1; # subclass the real Context so that we go through PLIF for everything sub new { my $class = shift; my($app, $output, $session, $protocol) = @_; my $self = $class->SUPER::new({ 'FILTERS' => { 'htmlcomment' => \&html_comment_filter, # for use in an html comment 'xmlcomment' => \&xml_comment_filter, # for use in an xml comment 'xml' => \&xml_filter, # for use in xml 'cdata' => \&cdata_filter, # for use in an xml CDATA block 'htmljs' => \&html_js_filter, # for use in strings in JS in HTML