########################################################################### # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved. # Please visit http://formbuilder.org for tutorials, support, and examples. ########################################################################### package CGI::FormBuilder::Template::TT2; =head1 NAME CGI::FormBuilder::Template::TT2 - FormBuilder interface to Template Toolkit =head1 SYNOPSIS my $form = CGI::FormBuilder->new( fields => \@fields, template => { type => 'TT2', template => 'form.tmpl', variable => 'form', } ); =cut use Carp; use strict; use warnings; no warnings 'uninitialized'; use CGI::FormBuilder::Util; use Template; our $VERSION = '3.09'; sub new { my $self = shift; my $class = ref($self) || $self; my $opt = arghash(@_); $opt->{engine} = Template->new($opt->{engine} || {}) || puke $Template::ERROR unless UNIVERSAL::isa($opt->{engine}, 'Template'); return bless $opt, $class; } sub engine { return shift()->{engine}; } sub render { my $self = shift; my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)"; my $tt2template = $self->{template} || puke "Template Toolkit template not specified"; my $tt2data = $self->{data} || {}; my $tt2var = $self->{variable}; # optional var for nesting if ($tt2var) { $tt2data->{$tt2var} = $tvar; } else { $tt2data = { %$tt2data, %$tvar }; } my $tt2output; # growing a scalar is so C-ish $self->{engine}->process($tt2template, $tt2data, \$tt2output) || puke $self->{engine}->error(); # string HTML output return $tt2output; } 1; __END__ =head1 DESCRIPTION This engine adapts B to use C