[Templates-cvs] cvs commit: TT3/lib Template.pm
cvs@template-toolkit.org
cvs@template-toolkit.org
Fri, 10 Dec 2004 15:05:21 +0000
cvs 04/12/10 15:05:21
Added: lib Template.pm
Log:
* added Template.pm front-end module
Revision Changes Path
1.1 TT3/lib/Template.pm
Index: Template.pm
===================================================================
#========================================================================
#
# Template
#
# DESCRIPTION
# Front-end module to the Template Toolkit.
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
# COPYRIGHT
# Copyright (C) 1996-2004 Andy Wardley. All Rights Reserved.
#
# This module is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# REVISION
# $Id: Template.pm,v 1.1 2004/12/10 15:05:21 abw Exp $
#
#========================================================================
package Template;
use strict;
use warnings;
use Template::Resources;
use Template::Context;
use Template::Utils;
use Template::Base;
use base qw( Template::Base );
our $VERSION = 'TT3-alpha-01';
our $DEBUG = 0 unless defined $DEBUG;
our $ERROR = '';
our $OUTPUT = \*STDOUT unless defined $OUTPUT;
our $RESOURCES = 'Template::Resources' unless defined $RESOURCES;
our $CONTEXT = 'Template::Context' unless defined $CONTEXT;
our $UTILS = 'Template::Utils' unless defined $UTILS;
sub init {
my ($self, $config) = @_;
$self->init_debugging($config) || return
if $config->{ debug };
$self->{ output } = delete $config->{ output }
|| $self->pkgvar( OUTPUT => $OUTPUT );
$self->{ output_path } = delete $config->{ output_path };
my $context = delete $config->{ context }
|| $self->pkgvar( CONTEXT => $CONTEXT );
$self->{ context } = $context->new($config)
|| return $self->error($context->error());
return $self;
}
sub init_debugging {
my $self = shift;
$self->debug("init_debugging() not yet implemented\n");
return $self;
}
sub context {
return $_[0]->{ context };
}
sub process {
my ($self, $path, $vars, $output) = splice(@_, 0, 4);
my $opts = @_ == 1 && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
my $context = $self->{ context };
my $result = $context->include($path, $vars)
|| return $self->error($context->error());
return $self->output($result, $output, $opts);
}
sub output {
my ($self, $textref, $output) = splice(@_, 0, 3);
my $opts = @_ == 1 && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
$output ||= $self->{ output };
my $outref = ref($output);
if ($outref eq 'CODE') {
# call a CODE reference
return &$output($$textref);
}
elsif ($outref eq 'GLOB') {
# print to a glob (such as \*STDOUT)
return print $output $$textref;
}
elsif ($outref eq 'SCALAR') {
# append output to a SCALAR ref
$$output .= $$textref;
return 1;
}
elsif ($outref eq 'ARRAY') {
# push onto ARRAY ref
push @$output, $$textref;
return 1;
}
elsif (UNIVERSAL::can($output, 'print')) {
# call the print() method on an object that implements the method
# (e.g. IO::Handle, Apache::Request, etc)
return $output->print($$textref);
}
elsif (! $outref) {
# non-reference $output is a filename, relative to
# any output_path that may (or may not) be defined
$output = $UTILS->file_path($self->{ output_path }, $output)
if $self->{ output_path };
return $UTILS->save_file($output, $textref, $opts)
|| $self->error($UTILS->error());
}
else {
return $self->error("invalid output specified: $output");
}
}
1;
__END__
=head1 NAME
Template - Perl interface to the Template Toolkit
=head1 SYNOPSIS
use Template;
=head1 DESCRIPTION
TODO
=head1 METHODS
TODO
=head1 AUTHOR
Andy Wardley E<lt>abw@wardley.orgE<gt>
=head1 VERSION
$Revision: 1.1 $
=head1 COPYRIGHT
Copyright (C) 1996-2004 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: