| 1 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 2 | # DynamicTemplate.pm | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | package HTML::DynamicTemplate; | ||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 2 | $VERSION = "0.94"; | |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | HTML::DynamicTemplate - HTML template class. | ||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | use HTML::DynamicTemplate; | ||||||||||||||||||||||||||||||||||||
| 21 | my $template = new HTML::DynamicTemplate 'path/to/template'; | ||||||||||||||||||||||||||||||||||||
| 22 | $template->set_recursion_limit($integer); | ||||||||||||||||||||||||||||||||||||
| 23 | $template->set(NAME => $value); | ||||||||||||||||||||||||||||||||||||
| 24 | $template->set(NAME_1 => $value_1, | ||||||||||||||||||||||||||||||||||||
| 25 | NAME_2 => $value_2, | ||||||||||||||||||||||||||||||||||||
| 26 | NAME_3 => $value_3); | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | $template->clear(); | ||||||||||||||||||||||||||||||||||||
| 29 | $template->render(); | ||||||||||||||||||||||||||||||||||||
| 30 | $template->render(@variables); | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | path/to/template | ||||||||||||||||||||||||||||||||||||
| 33 | ---------------- | ||||||||||||||||||||||||||||||||||||
| 34 | <html> | ||||||||||||||||||||||||||||||||||||
| 35 | <body> | ||||||||||||||||||||||||||||||||||||
| 36 | <h1>$HEADING</h1> | ||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | <p><font color="$HIGHLIGHT_COLOR">This is standard HTML with | ||||||||||||||||||||||||||||||||||||
| 39 | arbitrary embedded variable references which are substituted | ||||||||||||||||||||||||||||||||||||
| 40 | with actual values when the template is rendered.</font></p> | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | <p>Template variables may be set within the template itself | ||||||||||||||||||||||||||||||||||||
| 43 | with the special $SET directive. This is useful when setting | ||||||||||||||||||||||||||||||||||||
| 44 | variables for use by included templates. Example: | ||||||||||||||||||||||||||||||||||||
| 45 | $SET(PAGE_TITLE, "What's New"). Note: Be sure to escape | ||||||||||||||||||||||||||||||||||||
| 46 | quotes (") and closing parantheses ()) as HTML | ||||||||||||||||||||||||||||||||||||
| 47 | entities.</p> | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | <p>Additionally, templates may be recursively included by | ||||||||||||||||||||||||||||||||||||
| 50 | specifying a template with the special $INCLUDE directive. | ||||||||||||||||||||||||||||||||||||
| 51 | Example: $INCLUDE(templates/example.tmpl). Template paths may | ||||||||||||||||||||||||||||||||||||
| 52 | be variable references as in $INCLUDE($EXAMPLE_FILE). Note: | ||||||||||||||||||||||||||||||||||||
| 53 | Any variable references found in included templates will be | ||||||||||||||||||||||||||||||||||||
| 54 | substituted as in the original template. | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | <p>Usage note: variable and directive names are always | ||||||||||||||||||||||||||||||||||||
| 57 | specified in uppercase.</p> | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | </body> | ||||||||||||||||||||||||||||||||||||
| 60 | </html> | ||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | The C<HTML::DynamicTemplate> is a class implementing a HTML | ||||||||||||||||||||||||||||||||||||
| 65 | template in perl. Significant features include the ability to set | ||||||||||||||||||||||||||||||||||||
| 66 | template variables from within the template itself, the ability to | ||||||||||||||||||||||||||||||||||||
| 67 | recursively include other templates, and the ability to selectively | ||||||||||||||||||||||||||||||||||||
| 68 | render a specified subset of variables. | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | =over 4 | ||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | =item $template = new HTML::DynamicTemplate $template_filename; | ||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | Constructor for the template. Returns a reference to a | ||||||||||||||||||||||||||||||||||||
| 81 | HTML::DynamicTemplate object based on the specified template file. | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 86 | 2 | my($class, $template) = @_; | |||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | 2 | my $self = {}; | |||||||||||||||||||||||||||||||||||
| 89 | 2 | bless $self, $class; | |||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 2 | $self->{'vars'} = {}; | |||||||||||||||||||||||||||||||||||
| 92 | 2 | $self->{'source'} = ''; | |||||||||||||||||||||||||||||||||||
| 93 | 2 | $self->{'recursion_level'} = 0; | |||||||||||||||||||||||||||||||||||
| 94 | 2 | $self->{'recursion_limit'} = 10; | |||||||||||||||||||||||||||||||||||
| 95 | 2 | $self->{'template'} = $template; | |||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | open TEMPLATE, $template or die $!; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | while(<TEMPLATE>) { $self->{'source'} .= $_ } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | 2 | close TEMPLATE; | |||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||
| 101 | 2 | return $self; | |||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | =item $template->set_recursion_limit($integer); | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | A default recursion limit for template includes is implemented to | ||||||||||||||||||||||||||||||||||||
| 111 | prevent infinite recursions. Use this method to override the | ||||||||||||||||||||||||||||||||||||
| 112 | default value (10). | ||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub set_recursion_limit { | ||||||||||||||||||||||||||||||||||
|
Subroutine
set_recursion_limit: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 117 | 2 | my($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | $self->{'recursion_limit'} = $limit | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 120 | unless $limit !~ m/^\d+$/; | ||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||
| 122 | 2 | return; | |||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||
| 125 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | =item $template->set(NAME => $value); | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | Sets template variable to given value. | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 134 | 8 | my($self, @arguments) = @_; | |||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | 8 | ▶ | while(my $name = shift @arguments) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 137 | 28 | my $value = shift @arguments; | |||||||||||||||||||||||||||||||||||
| 138 | 28 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 139 | 28 | $self->{'vars'}{uc $name} = $value; | |||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||
| 142 | 8 | return; | |||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | =item $template->clear(); | ||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | Clears template variables. Useful when processing table row | ||||||||||||||||||||||||||||||||||||
| 150 | templates. | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | 2 | ▶ | sub clear { | ||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 155 | 2 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | 2 | $self->{'vars'} = {}; | |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | 2 | return; | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||
| 164 | =item $template->render(); | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | Renders template by performing variable substitutions. | ||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||
| 170 | =item $template->render(@variables); | ||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||
| 172 | Renders template by performing variable substitutions on only those | ||||||||||||||||||||||||||||||||||||
| 173 | variable names specified in @variables. | ||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||
| 177 | 2 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 178 | 2 | my($self, @variables) = @_; | |||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||
| 180 | 2 | $self->{'recursion_level'} = 0; | |||||||||||||||||||||||||||||||||||
| 181 | 2 | return $self->_substitute($self->{'source'}, @variables); | |||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | 218 | ▶ | sub _substitute { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_substitute: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||
| 189 | 218 | my($self, $source, @variables) = @_; | |||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 218 | $source =~ s/\$([0-9_A-Z]+)(\(([^)]+)\))?(\n?)/ | |||||||||||||||||||||||||||||||||||
| 192 | 146 | ▶ | if($1 eq 'SET' and defined $2) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$1 eq "SET" and defined $2
MC/DC100%2/2
$1 eq "SET" and defined $2
$1 eq "SET"
defined $2
Truth table100%3/3
A and B
A= $1 eq "SET"B= defined $2
| |||||||||||||||||||||||||||||||||||||
| 193 | 6 | ▶ | if($3 =~ m%^([0-9_A-Z]+)\s*,\s*"([^"]+)"$%) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 194 | 6 | $self->{'vars'}{$1} = $2 | |||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||
| 196 | 6 | ""; | |||||||||||||||||||||||||||||||||||
| 197 | ▶ | } elsif($1 eq 'INCLUDE' and defined $2) { | |||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$1 eq "INCLUDE" and defined $2
MC/DC100%2/2
$1 eq "INCLUDE" and defined $2
$1 eq "INCLUDE"
defined $2
Truth table100%3/3
A and B
A= $1 eq "INCLUDE"B= defined $2
| |||||||||||||||||||||||||||||||||||||
| 198 | 110 | ▶ | if($self->{'recursion_level'} < $self->{'recursion_limit'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 199 | 108 | $self->_include($self->_substitute($3), @variables).$4 | |||||||||||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||||||||||
| 201 | 2 | "[ include recursion limit exceeded ]$4" | |||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||
| 203 | } elsif($#variables > 0) { | ||||||||||||||||||||||||||||||||||||
| 204 | 14 | ▶ | if($self->_is_in_array($1, @variables)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 205 | 14 | $self->{'vars'}{$1}.$4 | |||||||||||||||||||||||||||||||||||
| 206 | } else { | ||||||||||||||||||||||||||||||||||||
| 207 | 0 | $4 | |||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||||||||||
| 210 | 16 | $self->{'vars'}{$1}.$4 | |||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||
| 212 | /egm; | ||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||
| 214 | 218 | return $source; | |||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||
| 217 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||
| 219 | 108 | ▶ | sub _include { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_include: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 220 | 108 | my($self, $template, @variables) = @_; | |||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||
| 222 | 108 | my $source; | |||||||||||||||||||||||||||||||||||
| 223 | 108 | ▶ | open TEMPLATE, $template or return "[ $template: $! ]"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 224 | 108 | ▶ | while(<TEMPLATE>) { $source .= $_ } | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 225 | 108 | close TEMPLATE; | |||||||||||||||||||||||||||||||||||
| 226 | 108 | chomp $source; | |||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||
| 228 | 108 | $self->{'recursion_level'}++; | |||||||||||||||||||||||||||||||||||
| 229 | 108 | $source = $self->_substitute($source, @variables); | |||||||||||||||||||||||||||||||||||
| 230 | 108 | $self->{'recursion_level'}--; | |||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||
| 232 | 108 | return $source; | |||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||
| 235 | #================================================================== | ||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||
| 237 | 14 | ▶ | sub _is_in_array { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_is_in_array: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||
| 238 | 14 | my($self, $element, @array) = @_; | |||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | 14 | for my $index (0..$#array) { | |||||||||||||||||||||||||||||||||||
| 241 | 65 | ▶ | return 1 unless $element ne $array[$index]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||
| 244 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | 2 | 1; | |||||||||||||||||||||||||||||||||||
| 250 | __END__ | ||||||||||||||||||||||||||||||||||||