blib/lib/CGI/FormBuilder/Template/Text.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 119 SCAR 37.4
CC
6
Cov
0%
render
30.0
new
6.0
engine
2.0
CRAP 42.0
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 package CGI::FormBuilder::Template::Text;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Template::Text - FormBuilder interface to Text::Template
12  
13 =head1 SYNOPSIS
14  
15 my $form = CGI::FormBuilder->new(
16 fields => \@fields,
17 template => {
18 type => 'Text',
19 template => 'form.tmpl',
20 variable => 'form',
21 }
22 );
23  
24 =cut
25  
26 use Carp;
27 use strict;
28 use warnings;
29 no warnings 'uninitialized';
30  
31 use CGI::FormBuilder::Util;
32 use Text::Template;
33  
34  
35 our $VERSION = '3.10';
36  
37 sub new {
38 my $self = shift;
39 my $class = ref($self) || $self;
40 my $opt = arghash(@_);
41  
42 my $tt_engine = $opt->{engine} || {};
43 unless (UNIVERSAL::isa($tt_engine, 'Text::Template')) {
44 $tt_engine->{&tt_param_name('type',%$tt_engine)} ||= $opt->{TYPE} || 'FILE';
45 $tt_engine->{&tt_param_name('source',%$tt_engine)} ||= $opt->{template} || $opt->{source} ||
46 puke "Text::Template source not specified, use the 'template' option";
47 $tt_engine->{&tt_param_name('delimiters',%$tt_engine)} ||= [ '<%','%>' ];
48 $opt->{engine} = Text::Template->new(%$tt_engine) || puke $Text::Template::ERROR;
49 }
50  
51 return bless $opt, $class;
52 }
53  
54 sub engine {
55 return shift()->{engine};
56 }
57  
58 # This sub helps us to support all of Text::Template's argument naming conventions
59 sub tt_param_name {
60 my ($arg, %h) = @_;
61 my ($key) = grep { exists $h{$_} } ($arg, "\u$arg", "\U$arg", "-$arg", "-\u$arg", "-\U$arg");
62 return $key || $arg;
63 }
64  
65 sub render {
66 my $self = shift;
67 my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)";
68  
69 my $tt_data;
70 if (ref $self->{data} eq 'ARRAY') {
71 $tt_data = $self->{data};
72 } else {
73 $tt_data = [ $self->{data} ];
74 }
75 my $tt_var = $self->{variable}; # optional var for nesting
76  
77 if ($tt_var) {
78 push @$tt_data, { $tt_var => $tvar };
79 } else {
80 push @$tt_data, $tvar;
81 }
82  
83 my $tt_fill_in = $self->{fill_in} || {};
84 my $tt_fill_in_hash = $tt_fill_in->{&tt_param_name('hash',%$tt_fill_in)} || {};
85 if (ref($tt_fill_in_hash) eq 'ARRAY') {
86 push @$tt_fill_in_hash, @$tt_data;
87 } else {
88 $tt_fill_in_hash = [ $tt_fill_in_hash, @$tt_data ];
89 }
90  
91 $tt_fill_in_hash = {} unless @$tt_fill_in_hash;
92 $tt_fill_in->{&tt_param_name('hash',%$tt_fill_in)} = $tt_fill_in_hash;
93  
94 my $tt_output = $self->{engine}->fill_in(%$tt_fill_in)
95 || puke "Text::Template expansion failed: $Text::Template::ERROR";
96  
97 return $tt_output;
98 }
99  
100 1;
101 __END__