Template/Bundle.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Template/ESCAPE.pm ↑ Summary Template/Preload.pm »
1 package HTML::Template::Bundle;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use HTML::Template::Expr '0.07_01';
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use base 'HTML::Template::Expr';
7 use Exporter qw(import);
8 use HTML::Template::Filters;
9 our $VERSION = '0.01';
10 our @EXPORT = qw(tmpl_render);
11 our $DEBUG = int(defined $ENV{DEBUG} ? $ENV{DEBUG} : 0) unless $DEBUG;
12  
13 sub new {
14 my $proto = shift;
15 my $class = ref($proto) || $proto;
16  
17 my $options = {};
18 $options = HTML::Template::_load_supplied_options([@_], $options);
19 $options->{extended_syntax} = 1;
20  
21 # Should we associate the environmental variables? If the value is set
22 # to a string, that is the prefix otherwise use the syntax ENV.xxx (we
23 # have already enabled structure_vars).
24 my $env = delete $options->{env_variables};
25  
26 # create the instance
27 my $self = $class->SUPER::new(%$options);
28 die "Failed to create ".__PACKAGE__ unless $self;
29  
30 # Pump in the environmental variables
31 if ($env) {
32 my %pmap = map { $_ => 1 } $self->param();
33 $env = "ENV" if ($env =~ /^\d+$/);
34 my $sep = $options->{structure_vars} ? "." : "_";
35 foreach my $k (keys %ENV) {
36 my $key = $env.$sep.$k;
37 $key = lc($key) unless $options->{case_sensitive};
38 exists $pmap{$key} && $self->param($key => $ENV{$k});
39 }
40 }
41  
42 return bless $self, $class;
43 }
44  
45 sub new_bundle {
46 my $proto = shift;
47 my $class = ref($proto) || $proto;
48  
49 my $options = {
50 die_on_bad_params => 0,
51 die_on_unset_params => 1,
52 structure_vars => 1,
53 loop_context_vars => 1,
54 intrinsic_vars => 1,
55 recursive_templates => -1,
56 strict => 0,
57 filter => HTML::Template::Filters->get_filters(
58 HT_FILTER_ALLOW_TRAILING_SLASH,
59 HT_FILTER_SSI_INCLUDE_VIRTUAL,
60 HT_FILTER_PERCENT_VARIABLES,
61 HT_FILTER_TMPL_CONSTANT,
62 HT_FILTER_TMPL_COMMENT,
63 HT_FILTER_TMPL_FIXME,
64 HT_FILTER_TMPL_JOIN,
65 $DEBUG ? HT_FILTER_STRIP_TMPL_NEWLINE_WHITESPACE : HT_FILTER_STRIP_REDUNDANT,
66 ),
67 };
68 $options = HTML::Template::_load_supplied_options([@_], $options);
69  
70 my $self = $class->new(%$options);
71 $self->param(DEBUG => $DEBUG);
72  
73 return $self;
74 }
75  
76 #--------------------------------------------------------------------------
77 # Override the default handling of parsing of unknown
78 # TMPL_xxx tags. Tags we know about:
79 #
80 # TMPL_SET
81 #
82 sub handle_tmpl_construct {
83 my ($self,$slash,$which,$part,$post,$pmap,$top_pmap) = @_;
84 my $options = $self->{options};
85 print STDERR "Handling custom TMPL_xxx construct:$/- TMPL_xxx: $slash$which$/- TMPL part: $part$/- post: $post$/ $/" if $options->{debug};
86  
87 if ($which eq 'SET') {
88  
89 # Translation of string and any template variables
90 if ($part =~ /^\s*
91  
92 # name = value
93 ([^=]+) # $1 => name
94 \s*=\s*
95 (
96 (?:"[^"]*")
97 |
98 (?:'[^']*')
99 |
100 (?:[^\s]*)
101 ) # $2 => value
102  
103 $/sxo) {
104 my $name = $1;
105 my $value = defined $2 ? $2 : '';
106 if (defined $name and length $name) {
107 $self->{set_stack} = [] unless $self->{set_stack};
108 push @{$self->{set_stack}}, [$name,$value];
109 return undef,$post;
110 }
111 print STDERR "Failed handling <TMPL_$which ...> - unknown key $/";
112 return undef,$post;
113 }
114  
115 print STDERR "Failed handling <".$slash."TMPL_$which \"$part\"> - incorrect syntax $/";
116 return undef,$post;
117 }
118  
119 return $self->SUPER::handle_tmpl_construct($slash,$which,$part,$post,$pmap,$top_pmap);
120 }
121  
122 ## overload default handling of ouput generation for custom TMPL_xxx tags
123 sub handle_parse_stack_construct {
124 my ($self,$index,$type,$tmpl_obj,$force_untaint) = @_;
125 my $options = $self->{options};
126 return $self->SUPER::handle_parse_stack_construct($index,$type,$tmpl_obj,$force_untaint);
127 }
128  
129 ## Overload _init() so that we pump out the TMPL_SET param()'s
130 sub _init {
131 my $self = shift;
132 $self->SUPER::_init(@_);
133 if ($self->{set_stack}) {
134 foreach my $set (@{$self->{set_stack}}) {
135 $self->param($set->[0] => $set->[1]);
136 }
137 }
138 }
139  
140 ## Simple support for batch/CGI processing
141 sub tmpl_render {
142 my $tmplfile = shift;
143 my $htmlfile = defined wantarray ? undef : shift;
144 croak("Incorrect num of params to tmpl_render") unless (@_ % 2 == 0);
145 my $path = [];
146 if ($tmplfile =~ /(.*)\/(.*)$/) {
147 push @$path, $1;
148 }
149 my $ht = __PACKAGE__->new_bundle(
150 filename => $tmplfile,
151 path => $path,
152 search_path_on_include => 1,
153 );
154 for($. = 0; $. < scalar(@_); $. += 2) {
155 my $key = $_[$.];
156 my $val = $_[$.+1];
157 $ht->param($key => $val);
158 my $r = HTML::Template::reftype($val);
159 if ($r eq 'ARRAY') {
160 $val = scalar(@$val);
161 } elsif ($r =~ 'HASH') {
162 $val = scalar(keys %$val);
163 } elsif (defined $val) {
164 $val = length $val;
165 } else {
166 $val = 0;
167 }
168 $ht->param($key.".length" => $val);
169 }
170 my $content = $ht->output();
171 if ($htmlfile) {
172 open(FH,">","$htmlfile.tmp") or die $!;
173 print FH $content;
174 close(FH);
175 rename("$htmlfile.tmp","$htmlfile") or die $!;
176 }
177 return $content;
178 }
179  
180 1;
181 __END__
« Template/ESCAPE.pm ↑ Summary Template/Preload.pm »