1#!/usr/bin/perl -w
2use strict;
3use Test::More qw(no_plan);
4
5use_ok('HTML::Template::Pro');
6
7my ($output, $template, $result);
8
9# test a simple template
10$template = HTML::Template->new(
11                                   path => 'templates',
12                                   filename => 'simple.tmpl',
13                                   debug => 0
14                                  );
15
16$template->param('ADJECTIVE', 'very');
17$output =  $template->output;
18ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very');
19
20# try something a bit larger
21$template = HTML::Template->new(
22                                path => 'templates',
23                                filename => 'medium.tmpl',
24                                # debug => 1,
25                                      );
26$template->param('ALERT', 'I am alert.');
27$template->param('COMPANY_NAME', "MY NAME IS");
28$template->param('COMPANY_ID', "10001");
29$template->param('OFFICE_ID', "10103214");
30$template->param('NAME', 'SAM I AM');
31$template->param('ADDRESS', '101011 North Something Something');
32$template->param('CITY', 'NEW York');
33$template->param('STATE', 'NEw York');
34$template->param('ZIP','10014');
35$template->param('PHONE','212-929-4315');
36$template->param('PHONE2','');
37$template->param('SUBCATEGORIES','kfldjaldsf');
38$template->param('DESCRIPTION',"dsa;kljkldasfjkldsajflkjdsfklfjdsgkfld\nalskdjklajsdlkajfdlkjsfd\n\talksjdklajsfdkljdsf\ndsa;klfjdskfj");
39$template->param('WEBSITE','http://www.assforyou.com/');
40$template->param('INTRANET_URL','http://www.something.com');
41$template->param('REMOVE_BUTTON', "<INPUT TYPE=SUBMIT NAME=command VALUE=\"Remove Office\">");
42$template->param('COMPANY_ADMIN_AREA', "<A HREF=administrator.cgi?office_id={office_id}&command=manage>Manage Office Administrators</A>");
43$template->param('CASESTUDIES_LIST', "adsfkljdskldszfgfdfdsgdsfgfdshghdmfldkgjfhdskjfhdskjhfkhdsakgagsfjhbvdsaj hsgbf jhfg sajfjdsag ffasfj hfkjhsdkjhdsakjfhkj kjhdsfkjhdskfjhdskjfkjsda kjjsafdkjhds kjds fkj skjh fdskjhfkj kj kjhf kjh sfkjhadsfkj hadskjfhkjhs ajhdsfkj akj fkj kj kj  kkjdsfhk skjhadskfj haskjh fkjsahfkjhsfk ksjfhdkjh sfkjhdskjfhakj shiou weryheuwnjcinuc 3289u4234k 5 i 43iundsinfinafiunai saiufhiudsaf afiuhahfwefna uwhf u auiu uh weiuhfiuh iau huwehiucnaiuncianweciuninc iuaciun iucniunciunweiucniuwnciwe");
44$template->param('NUMBER_OF_CONTACTS', "aksfjdkldsajfkljds");
45$template->param('COUNTRY_SELECTOR', "klajslkjdsafkljds");
46$template->param('LOGO_LINK', "dsfpkjdsfkgljdsfkglj");
47$template->param('PHOTO_LINK', "lsadfjlkfjdsgkljhfgklhasgh");
48
49$output = $template->output;
50ok($output !~ /<TMPL_VAR/);
51
52# test a simple loop template
53$template = HTML::Template->new(
54                                path => 'templates',
55                                filename => 'simple-loop.tmpl',
56                                # debug => 1,
57                               );
58$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
59
60$output =  $template->output;
61ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s);
62
63# test a simple loop template
64$template = HTML::Template->new(
65                                path => 'templates',
66                                filename => 'simple-loop-nonames.tmpl',
67                                # debug => 1,
68                               );
69$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
70
71$output =  $template->output;
72ok($output !~ /ADJECTIVE_LOOP/ and  $output =~ /really.*very/s);
73
74# test a long loop template - mostly here to use timing on.
75$template = HTML::Template->new(
76                                path => 'templates',
77                                filename => 'long_loops.tmpl',
78                                # debug => 1,
79                               );
80$output =  $template->output;
81ok(1);
82
83# test a template with TMPL_INCLUDE
84$template = HTML::Template->new(
85                                path => 'templates',
86                                filename => 'include.tmpl',
87                                # debug => 1,
88                               );
89$output =  $template->output;
90ok(not (!($output =~ /5/) || !($output =~ /6/)));
91
92# test a template with TMPL_INCLUDE and cacheing.
93$template = HTML::Template->new(
94                                path => 'templates',
95                                filename => 'include.tmpl',
96                                cache => 1,
97                                # cache_debug => 1,
98                                # debug => 1,
99                               );
100$output =  $template->output;
101ok(not (!($output =~ /5/) || !($output =~ /6/)));
102
103# stimulates a cache miss
104# system('touch templates/included2.tmpl');
105
106my $template2 = HTML::Template->new(
107                                    path => 'templates',
108                                    filename => 'include.tmpl',
109                                    cache => 1,
110                                    # cache_debug => 1,
111                                    # debug => 1,
112                                   );
113$output =  $template->output;
114ok(not (!($output =~ /5/) || !($output =~ /6/)));
115
116# test associate
117my $template_one = HTML::Template->new(
118                                       path => 'templates',
119                                       filename => 'simple.tmpl',
120                                       # debug => 1,
121                                      );
122$template_one->param('ADJECTIVE', 'very');
123
124my $template_two = HTML::Template->new (
125                                        path => 'templates',
126                                        filename => 'simple.tmpl',
127                                        associate => $template_one,
128                                        # debug => 1,
129                                       );
130
131$output =  $template_two->output;
132ok($output !~ /ADJECTIVE/ and $output =~ /very/);
133
134# test a simple loop template
135my $template_l = HTML::Template->new(
136                                     path => 'templates',
137                                     filename => 'other-loop.tmpl',
138                                     # debug => 1,
139                                  );
140# $template_l->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
141
142$output =  $template_l->output;
143ok($output !~ /INSIDE/);
144
145# test a simple if template
146my $template_i = HTML::Template->new(
147                                     path => 'templates',
148                                     filename => 'if.tmpl',
149                                     # debug => 1,
150                                  );
151# $template_l->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
152
153$output =  $template_i->output;
154ok($output !~ /INSIDE/);
155
156# test a simple if template
157my $template_i2 = HTML::Template->new(
158                                      path => 'templates',
159                                      filename => 'if.tmpl',
160                                      # stack_debug => 1,
161                                      # debug => 1,
162                                  );
163$template_i2->param(BOOL => 1);
164
165$output =  $template_i2->output;
166ok($output =~ /INSIDE/);
167
168# test a simple if/else template
169my $template_ie = HTML::Template->new(
170                                      path => 'templates',
171                                      filename => 'ifelse.tmpl',
172                                      # debug => 1,
173                                     );
174
175$output =  $template_ie->output;
176ok ($output =~ /INSIDE ELSE/);
177
178# test a simple if/else template
179my $template_ie2 = HTML::Template->new(
180                                       path => 'templates',
181                                       filename => 'ifelse.tmpl',
182                                       # debug => 1,
183                                      );
184$template_ie2->param(BOOL => 1);
185
186$output =  $template_ie2->output;
187ok($output =~ /INSIDE IF/ and $output !~ /INSIDE ELSE/);
188
189# test a bug involving two loops with the same name
190$template = HTML::Template->new(
191                                path => 'templates',
192                                filename => 'double_loop.tmpl',
193                                # debug => 1,
194                               );
195$template->param('myloop', [
196                            { var => 'first'},
197                            { var => 'second' },
198                            { var => 'third' }
199                           ]
200                );
201$output = $template->output;
202ok($output =~ /David/);
203
204# test escapeing
205$template = HTML::Template->new(
206                                path => 'templates',
207                                filename => 'escape.tmpl',
208                                # debug => 1,
209                               );
210$template->param(STUFF => '<>"\''); #"
211$output = $template->output;
212ok($output !~ /[<>"']/); #"
213
214# test a simple template, using new param() call format
215$template = HTML::Template->new(
216                                path => 'templates',
217                                filename => 'simple.tmpl',
218                                # debug => 1,
219                               );
220
221$template->param(
222                 {
223                  'ADJECTIVE' => 'very'
224                 }
225                );
226$output =  $template->output;
227ok($output !~ /ADJECTIVE/ and $output =~ /very/);
228
229SKIP: {
230  skip "Skipping die test. Not supported in Pro\n", 1
231    if (!exists($ENV{TEST_DIE}) or !$ENV{TEST_DIE});
232# test a recursively including template
233eval {
234  $template = HTML::Template->new(
235                                  path => 'templates',
236                                  filename => 'recursive.tmpl',
237                                 );
238
239  $output =  $template->output;
240};
241
242ok(defined($@) and ($@ =~ /recursive/));
243}
244
245# test a template using unless
246$template = HTML::Template->new(
247                                path => 'templates',
248                                filename => 'unless.tmpl',
249                                # debug => 1
250                               );
251$template->param(BOOL => 1);
252
253$output =  $template->output;
254ok($output !~ /INSIDE UNLESS/ and $output =~ /INSIDE ELSE/);
255
256# test a template using unless
257$template = HTML::Template->new(
258                                path => 'templates',
259                                filename => 'unless.tmpl',
260                                #debug => 1,
261                                #debug_stack => 1
262                               );
263$template->param(BOOL => 0);
264
265$output =  $template->output;
266ok($output =~ /INSIDE UNLESS/ and $output !~ /INSIDE ELSE/);
267
268# test a template using loop_context_vars
269$template = HTML::Template->new(
270                                path => 'templates',
271                                filename => 'context.tmpl',
272                                loop_context_vars => 1,
273                                #debug => 1,
274                                #debug_stack => 1
275                               );
276$template->param(FRUIT => [
277                           {KIND => 'Apples'},
278                           {KIND => 'Oranges'},
279                           {KIND => 'Brains'},
280                           {KIND => 'Toes'},
281                           {KIND => 'Kiwi'}
282                          ]);
283$template->param(PINGPONG => [ {}, {}, {}, {}, {}, {} ]);
284
285$output =  $template->output;
286ok($output =~ /Apples, Oranges, Brains, Toes, and Kiwi./ and $output =~ /pingpongpingpongpingpong/);
287
288$template = HTML::Template->new(
289                                path => 'templates',
290                                filename => 'loop-if.tmpl',
291                                #debug => 1,
292                                #debug_stack => 1
293                               );
294$output =  $template->output;
295ok($output =~ /Loop not filled in/);
296
297$template = HTML::Template->new(
298                                path => 'templates',
299                                filename => 'loop-if.tmpl',
300                                #debug => 1,
301                                #debug_stack => 1
302                               );
303$template->param(LOOP_ONE => [{VAR => "foo"}]);
304$output =  $template->output;
305ok($output !~ /Loop not filled in/);
306
307# test shared memory - enable by setting the environment variable
308# TEST_SHARED_MEMORY to 1.
309SKIP: {
310  skip "Skipping shared memory cache test.  See README to enable\n", 2
311    if (!exists($ENV{TEST_SHARED_MEMORY}) or !$ENV{TEST_SHARED_MEMORY});
312
313
314  require 'IPC/SharedCache.pm';
315  my $template_prime = HTML::Template->new(
316                                           filename => 'simple-loop.tmpl',
317                                           path => ['templates/'],
318                                           shared_cache => 1,
319                                           ipc_key => 'TEST',
320                                           #cache_debug => 1,
321                                          );
322
323  my $template = HTML::Template->new(
324                                     filename => 'simple-loop.tmpl',
325                                     path => ['templates/'],
326                                     shared_cache => 1,
327                                     ipc_key => 'TEST',
328                                     #cache_debug => 1,
329                                    );
330  $template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
331  $output =  $template->output;
332  ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s);
333
334   my $template_prime2 = HTML::Template->new(
335                                            filename => 'simple-loop.tmpl',
336                                            path => ['templates/'],
337                                            double_cache => 1,
338                                            ipc_key => 'TEST',
339                                            #cache_debug => 1,
340                                     );
341
342   my $template2 = HTML::Template->new(
343                                      filename => 'simple-loop.tmpl',
344                                      path => ['templates/'],
345                                      double_cache => 1,
346                                      ipc_key => 'TEST',
347                                      #cache_debug => 1,
348                                     );
349   $template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
350   $output =  $template->output;
351   ok($output !~ /ADJECTIVE_LOOP/ and $output =~ /really.*very/s);
352
353  IPC::SharedCache::remove('TEST');
354}
355
356# test CGI associate bug
357eval { require 'CGI.pm'; };
358SKIP: {
359  skip "Skipping associate tests, need CGI.pm to test associate\n", 1
360    if ($@);
361
362  my $query = CGI->new('');
363  $query->param('AdJecTivE' => 'very');
364  $template = HTML::Template->new(
365                                     path => 'templates',
366                                     filename => 'simple.tmpl',
367                                     debug => 0,
368                                     associate => $query,
369                                    );
370  $output =  $template->output;
371  ok($output =~ /very/);
372}
373
374# test subroutine as VAR
375$template = HTML::Template->new(
376                                path => 'templates',
377                                filename => 'simple.tmpl',
378                                debug => 0,
379                               );
380$template->param(ADJECTIVE => sub { return 'v' . '1e' . '2r' . '3y'; });
381$output =  $template->output;
382ok($output =~ /v1e2r3y/);
383
384# test cache - non automated, requires turning on debug watching STDERR!
385$template = HTML::Template->new(
386                                path => ['templates/'],
387                                filename => 'simple.tmpl',
388                                cache => 1,
389                                # cache_debug => 1,
390                                debug => 0,
391                               );
392$template->param(ADJECTIVE => sub { return 'v' . '1e' . '2r' . '3y'; });
393$output =  $template->output;
394$template = HTML::Template->new(
395                                path => ['templates/'],
396                                filename => 'simple.tmpl',
397                                cache => 1,
398                                # cache_debug => 1,
399                                debug => 0,
400                               );
401ok($output =~ /v1e2r3y/);
402
403# test URL escapeing
404$template = HTML::Template->new(
405                                path => 'templates',
406                                filename => 'urlescape.tmpl',
407                                # debug => 1,
408                                # stack_debug => 1,
409                               );
410$template->param(STUFF => '<>"; %FA'); #"
411$output = $template->output;
412ok($output !~ /[<>"]/); #"
413
414SKIP: {
415  skip "Skipping query test. Not supported in Pro\n", 2
416    if (!exists($ENV{TEST_QUERY}) or !$ENV{TEST_QUERY});
417# test query()
418$template = HTML::Template->new(
419                                path => 'templates',
420                                filename => 'query-test.tmpl',
421                               );
422my %params = map {$_ => 1} $template->query(loop => 'EXAMPLE_LOOP');
423
424my @result;
425eval {
426  @result = $template->query(loop => ['EXAMPLE_LOOP', 'BEE']);
427};
428
429ok($@ =~ /error/ and
430   $template->query(name => 'var') eq 'VAR' and
431   $template->query(name => 'EXAMPLE_LOOP') eq 'LOOP' and
432   exists $params{bee} and
433   exists $params{bop} and
434   exists $params{example_inner_loop} and
435   $template->query(name => ['EXAMPLE_LOOP', 'EXAMPLE_INNER_LOOP']) eq 'LOOP'
436  );
437
438# test query()
439$template = HTML::Template->new(
440                                path => 'templates',
441                                filename => 'query-test2.tmpl',
442                               );
443my %p = map {$_ => 1} $template->query(loop => ['LOOP_FOO', 'LOOP_BAR']);
444ok(exists $p{foo} and exists $p{bar} and exists $p{bash});
445}
446
447# test global_vars
448$template = HTML::Template->new(
449                                path => 'templates',
450                                filename => 'globals.tmpl',
451                                global_vars => 1,
452                               );
453$template->param(outer_loop => [{loop => [{'LOCAL' => 'foo'}]}]);
454$template->param(global => 'bar');
455$template->param(hidden_global => 'foo');
456$result = $template->output();
457ok($result =~ /foobar/);
458
459SKIP: {
460  skip "Skipping vanguard test. Not supported in Pro\n", 1
461    if (!exists($ENV{TEST_VANGUARD}) or !$ENV{TEST_VANGUARD});
462$template = HTML::Template->new(
463                                path => 'templates',
464                                filename => 'vanguard1.tmpl',
465                                vanguard_compatibility_mode => 1,
466                               );
467$template->param(FOO => 'bar');
468$template->param(BAZ => 'bop');
469$result = $template->output();
470ok($result =~ /bar/ and $result =~ /bop/);
471}
472
473$template = HTML::Template->new(
474                                path => 'templates',
475                                filename => 'loop-context.tmpl',
476                                loop_context_vars => 1,
477                               );
478$template->param(TEST_LOOP => [ { NUM => 1 } ]);
479$result = $template->output();
480ok($result =~ /1:FIRST::LAST:ODD/);
481
482# test a TMPL_INCLUDE from a later path directory back up to an earlier one
483# when using the search_path_on_include option
484$template = HTML::Template->new(
485                                path => ['templates/searchpath/','templates/'],
486                                search_path_on_include => 1,
487                                filename => 'include.tmpl',
488                               );
489$output =  $template->output;
490ok($output =~ /9/ and $output =~ /6/);
491
492SKIP: {
493  skip "Skipping die test. Not supported in Pro\n", 1
494    if (!exists($ENV{TEST_DIE}) or !$ENV{TEST_DIE});
495# test no_includes
496eval {
497  $template = HTML::Template->new(
498                                  path => ['templates/'],
499                                  filename => 'include.tmpl',
500                                  no_includes => 1,
501                                 );
502};
503ok(defined $@ and $@ =~ /no_includes/);
504}
505
506# test file cache - non automated, requires turning on debug watching STDERR!
507$template = HTML::Template->new(
508                                path => ['templates/'],
509                                filename => 'simple.tmpl',
510                                file_cache_dir => './blib/temp_cache_dir',
511                                file_cache => 1,
512                                #cache_debug => 1,
513                                #debug => 0,
514                               );
515$template->param(ADJECTIVE => sub { "3y"; });
516$output =  $template->output;
517$template = HTML::Template->new(
518                                path => ['templates/'],
519                                filename => 'simple.tmpl',
520                                file_cache_dir => './blib/temp_cache_dir',
521                                file_cache => 1,
522                                #cache_debug => 1,
523                                #debug => 0,
524                               );
525ok($output =~ /3y/);
526
527my $x;
528$template = HTML::Template->new(filename => 'templates/simple-loop.tmpl',
529                                filter => {
530                                           sub => sub {
531                                               $x = 1;
532                                               for (@{$_[0]}) {
533                                                   $_ = "$x : $_";
534                                                   $x++;
535                                               }
536                                           },
537                                           format => 'array',
538                                          },
539                                file_cache_dir => './blib/temp_cache_dir',
540                                file_cache => 1,
541                                global_vars => 1,
542                               );
543$template->param('ADJECTIVE_LOOP', [ { ADJECTIVE => 'really' }, { ADJECTIVE => 'very' } ] );
544$output =  $template->output;
545ok($output =~ /very/);
546
547$template = HTML::Template->new(filename => './templates/include_path/a.tmpl');
548$output =  $template->output;
549ok($output =~ /Bar/);
550
551open(OUT, ">blib/test.out") or die $!;
552$template = HTML::Template->new(filename => './templates/include_path/a.tmpl');
553$template->output(print_to => *OUT);
554close(OUT);
555open(OUT, "blib/test.out") or die $!;
556$output = join('',<OUT>);
557close(OUT);
558ok($output =~ /Bar/);
559
560my $test = 39; # test with case sensitive params on
561my $template_source = <<END_OF_TMPL;
562  I am a <TMPL_VAR NAME="adverb"> <TMPL_VAR NAME="ADVERB"> simple template.
563END_OF_TMPL
564$template = HTML::Template->new(
565                                scalarref => \$template_source,
566                                case_sensitive => 1,
567                                debug => 0
568                               );
569
570$template->param('adverb', 'very');
571$template->param('ADVERB', 'painfully');
572$output =  $template->output;
573ok($output !~ /ADVERB/i and
574   $template->param('ADVERB') eq 'painfully' and
575   $template->param('adverb') eq 'very' and
576   $output =~ /very painfully/);
577
578# test with case sensitive params off
579$template_source = <<END_OF_TMPL;
580  I am a <TMPL_VAR NAME="adverb"> <TMPL_VAR NAME="ADVERB"> simple template.
581END_OF_TMPL
582$template = HTML::Template->new(
583                                scalarref => \$template_source,
584                                case_sensitive => 0,
585                                debug => 0
586                               );
587
588$template->param('adverb', 'very');
589$template->param('ADVERB', 'painfully');
590$output =  $template->output;
591ok($output !~ /ADVERB/i and
592   $template->param('ADVERB') eq 'painfully' and
593   $template->param('adverb') eq 'painfully' and
594   $output =~ /painfully painfully/);
595
596$template = HTML::Template->new(filename => './templates/include_path/a.tmpl',
597                                filter => sub {
598                                  ${$_[0]} =~ s/Bar/Zanzabar/g;
599                                }
600                               );
601$output =  $template->output;
602ok($output =~ /Zanzabar/);
603
604$template = HTML::Template->new(filename => './templates/include_path/a.tmpl',
605                                filter => [
606                                           {
607                                            sub => sub {
608                                              ${$_[0]} =~ s/Bar/Zanzabar/g;
609                                            },
610                                            format => 'scalar'
611                                           },
612                                           {
613                                            sub => sub {
614                                              ${$_[0]} =~ s/bar/bar!!!/g;
615                                            },
616                                            format => 'scalar'
617                                           }
618                                          ]
619                               );
620$output =  $template->output;
621ok($output =~ /Zanzabar!!!/);
622
623
624$template = HTML::Template->new(filename => './templates/include_path/a.tmpl',
625                                filter => {
626                                           sub => sub {
627                                             $x = 1;
628                                             for (@{$_[0]}) {
629                                               $_ = "$x : $_";
630                                               $x++;
631                                             }
632                                           },
633                                           format => 'array',
634                                          }
635                               );
636$output =  $template->output;
637ok($output =~ /1 : Foo/);
638
639$template = HTML::Template->new(
640                                scalarref => \ "\n<TMPL_INCLUDE templates/simple.tmpl>",
641                               );
642$template->param(ADJECTIVE => "3y");
643$output = $template->output();
644ok($output =~ /3y/);
645
646$template = HTML::Template->new(path => ['templates'],
647                                filename => 'newline_test1.tmpl',
648                                filter => sub {},
649                               );
650$output = $template->output();
651ok($output =~ /STARTincludeEND/);
652
653# test multiline tags
654$template = HTML::Template->new(path => ['templates'],
655                                filename => 'multiline_tags.tmpl',
656                                global_vars => 1,
657                               );
658$template->param(FOO => 'foo!', bar => [{}, {}]);
659$output = $template->output();
660ok($output =~ /foo!\n/ && $output =~ /foo!foo!\nfoo!foo!/);
661
662# test new() from filehandle
663open(TEMPLATE, "templates/simple.tmpl");
664$template = HTML::Template->new(filehandle => *TEMPLATE);
665
666$template->param('ADJECTIVE', 'very');
667$output =  $template->output;
668ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very');
669close(TEMPLATE);
670
671# test new_() from filehandle
672open(TEMPLATE, "templates/simple.tmpl");
673$template = HTML::Template->new_filehandle(*TEMPLATE);
674
675$template->param('ADJECTIVE', 'very');
676$output =  $template->output;
677ok($output !~ /ADJECTIVE/ and $template->param('ADJECTIVE') eq 'very');
678close(TEMPLATE);
679
680# test case sensitive loop variables
681$template = HTML::Template->new(path => ['templates'],
682                                filename => 'case_loop.tmpl',
683                                case_sensitive => 1,
684                               );
685$template->param(loop => [ { foo => 'bar', FOO => 'BAR' } ]);
686$output = $template->output();
687ok($output =~ /bar BAR/);
688
689# test ifs with code refd
690$template = HTML::Template->new(path => ['templates'],
691                                filename => 'if.tmpl');
692$template->param(bool => sub { 0 });
693$output = $template->output();
694ok($output !~ /INSIDE/ and $output =~ /unless/);
695
696# test global_vars for loops within loops
697$template = HTML::Template->new(path => ['templates'],
698				filename => 'global-loops.tmpl',
699				global_vars => 1);
700$template->param(global => "global val");
701$template->param(outer_loop => [
702				{
703				 foo => 'foo val 1',
704				 inner_loop => [
705						{ bar => 'bar val 1' },
706						{ bar => 'bar val 2' },
707					       ],
708				},
709				{
710				 foo => 'foo val 2',
711				 inner_loop => [
712						{ bar => 'bar val 3' },
713						{ bar => 'bar val 4' },
714					       ],
715				}
716			       ]);
717$output = $template->output;
718ok($output =~ /inner loop foo:    foo val 1/ and
719   $output =~ /inner loop foo:    foo val 2/);
720
721
722# test nested include path handling
723$template = HTML::Template->new(path => ['templates'],
724				   filename => 'include_path/one.tmpl');
725$output = $template->output;
726ok($output =~ /ONE/ and $output =~ /TWO/ and $output =~ /THREE/);
727
728# test using HTML_TEMPLATE_ROOT with path
729{
730    local $ENV{HTML_TEMPLATE_ROOT} = "templates";
731    $template = HTML::Template->new(
732                                    path => ['searchpath'],
733                                    filename => 'three.tmpl',
734                                   );
735    $output =  $template->output;
736    ok($output =~ /THREE/);
737}
738
739# test __counter__
740$template = HTML::Template->new(path              => ['templates'],
741				   filename          => 'counter.tmpl',
742                                   loop_context_vars => 1);
743$template->param(foo => [ {a => 'a'}, {a => 'b'}, {a => 'c'} ]);
744$template->param(outer => [ {inner => [ {a => 'a'}, {a => 'b'}, {a => 'c'} ] },
745                            {inner => [ {a => 'x'}, {a => 'y'}, {a => 'z'} ] },                           ]);
746$output = $template->output;
747ok($output =~ /^1a2b3c$/m);
748ok($output =~ /^11a2b3c21x2y3z$/m);
749
750# test default
751$template = HTML::Template->new(path              => ['templates'],
752				   filename          => 'default.tmpl');
753$template->param(cl => 'clothes');
754$template->param(start => 'start');
755$output = $template->output;
756ok($output =~ /cause it's getting hard to think, and my clothes are starting to shrink/);
757
758SKIP: {
759  skip "Skipping die test. Not supported in Pro\n", 1
760    if (!exists($ENV{TEST_DIE}) or !$ENV{TEST_DIE});
761# test invalid <tmpl_var>
762eval {
763    my $template = HTML::Template->new(scalarref => \'<tmpl_var>');
764};
765ok($@ =~ /No NAME given/);
766}
767
768
769# test a case where a different path should stimulate a cache miss
770# even though the main template is the same
771$template = HTML::Template->new(path => ['templates',
772                                            'templates/include_path'],
773                                   filename => 'outer.tmpl',
774                                   search_path_on_include => 1,
775                                   cache => 1,
776                                   # cache_debug => 1,
777                                  );
778$output = $template->output;
779ok($output =~ /I AM OUTER/);
780ok($output =~ /I AM INNER 1/);
781
782$template = HTML::Template->new(path => ['templates',
783                                            'templates/include_path2'],
784                                   filename => 'outer.tmpl',
785                                   search_path_on_include => 1,
786                                   cache => 1,
787                                   # cache_debug => 1,
788                                  );
789$output = $template->output;
790ok($output =~ /I AM OUTER/);
791ok($output =~ /I AM INNER 2/);
792
793# try the same thing with the file cache
794$template = HTML::Template->new(path => ['templates',
795                                            'templates/include_path'],
796                                   filename => 'outer.tmpl',
797                                   search_path_on_include => 1,
798                                   file_cache_dir => './blib/temp_cache_dir',
799                                   file_cache => 1,
800                                   # cache_debug => 1,
801                                  );
802$output = $template->output;
803ok($output =~ /I AM OUTER/);
804ok($output =~ /I AM INNER 1/);
805
806$template = HTML::Template->new(path => ['templates',
807                                            'templates/include_path2'],
808                                   filename => 'outer.tmpl',
809                                   search_path_on_include => 1,
810                                   file_cache_dir => './blib/temp_cache_dir',
811                                   file_cache => 1,
812                                   # cache_debug => 1,
813                                  );
814$output = $template->output;
815ok($output =~ /I AM OUTER/);
816ok($output =~ /I AM INNER 2/);
817
818# test javascript escaping
819$template = HTML::Template->new(path => ['templates'],
820                                            filename => 'js.tmpl');
821$template->param(msg => qq{"He said 'Hello'.\n\r"});
822$output = $template->output();
823is($output, q{\u0022He said \u0027Hello\u0027.\u000a\u000d\u0022});
824
825SKIP: {
826  skip "Skipping die test. Not supported in Pro\n", 1
827    if (!exists($ENV{TEST_DIE}) or !$ENV{TEST_DIE});
828# test empty filename
829eval { $template = $template = HTML::Template->new(path => ['templates'],
830                                                   filename => '');
831};
832like($@, qr/empty filename/);
833# end commented for Pro
834}
835
836# test default escaping
837
838#ok(exists $template->{options}->{default_escape} && !defined $template->{options}->{default_escape}, "default default_escape");
839
840$template = HTML::Template->new(path => ['templates'],
841                                            filename => 'default_escape.tmpl',
842                                            default_escape => 'UrL');
843#is($template->{options}->{default_escape}, 'URL');
844$template->param(STUFF => q{Joined with space});
845$output = $template->output();
846like($output, qr{^Joined%20with%20space});
847
848$template = HTML::Template->new(path => ['templates'],
849                                            filename => 'default_escape.tmpl',
850                                            default_escape => 'html');
851$template->param(STUFF => q{Joined&with"cruft});
852$template->param(LOOP => [ { MORE_STUFF => '<&>' }, { MORE_STUFF => '>&<' } ]);
853$template->param(a => '<b>');
854$output = $template->output();
855like($output, qr{^Joined&amp;with&quot;cruft});
856like($output, qr{&lt;&amp;&gt;&gt;&amp;&lt;});
857like($output, qr{because it's &lt;b&gt;});
858
859eval {
860$template = HTML::Template->new(path => ['templates'],
861                                            filename => 'default_escape.tmpl',
862                                            default_escape => 'wml');
863};
864#like($@, qr/Invalid setting for default_escape/);
865