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