/usr/share/perl5/Template/Alloy/TT.pm is in libtemplate-alloy-perl 1.020-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 | package Template::Alloy::TT;
=head1 NAME
Template::Alloy::TT - Template::Toolkit role
=cut
use strict;
use warnings;
use Template::Alloy;
use Template::Alloy::Operator qw($QR_OP_ASSIGN);
our $VERSION = $Template::Alloy::VERSION;
our $QR_COMMENTS;
use constant posessive => ($^V >= 5.009) || 0; # perl 5.10 allows possessive
sub new { die "This class is a role for use by packages such as Template::Alloy" }
###----------------------------------------------------------------###
sub parse_tree_tt3 {
my $self = shift;
my $str_ref = shift;
my $one_tag_only = shift() ? 1 : 0;
if (! $str_ref || ! defined $$str_ref) {
$self->throw('parse.no_string', "No string or undefined during parse", undef, 1);
}
my $STYLE = $self->{'TAG_STYLE'} || 'default';
local $self->{'_end_tag'} = $self->{'END_TAG'} || $Template::Alloy::Parse::TAGS->{$STYLE}->[1];
local $self->{'START_TAG'} = $self->{'START_TAG'} || $Template::Alloy::Parse::TAGS->{$STYLE}->[0];
local $self->{'_start_tag'} = (! $self->{'INTERPOLATE'}) ? $self->{'START_TAG'} : qr{(?: $self->{'START_TAG'} | (\$))}sx;
local $QR_COMMENTS = $QR_COMMENTS || (posessive() ? (local $Template::Alloy::Parse::QR_COMMENTS = "(?sm: \\s*+ \\# .*? (?: \$ | (?=$self->{'_end_tag'}) ) )*+ \\s*+") : $Template::Alloy::Parse::QR_COMMENTS);
my $dirs = $Template::Alloy::Parse::DIRECTIVES;
my $aliases = $Template::Alloy::Parse::ALIASES;
local @{ $dirs }{ keys %$aliases } = values %$aliases; # temporarily add to the table
local @{ $self }{@Template::Alloy::CONFIG_COMPILETIME} = @{ $self }{@Template::Alloy::CONFIG_COMPILETIME};
delete $dirs->{'JS'} if ! $self->{'COMPILE_JS'};
my @tree; # the parsed tree
my $pointer = \@tree; # pointer to current tree to handle nested blocks
my @state; # maintain block levels
local $self->{'_state'} = \@state; # allow for items to introspect (usually BLOCKS)
local $self->{'_no_interp'} = 0; # no interpolation in some blocks (usually PERL)
my @in_view; # let us know if we are in a view
my @blocks; # store blocks for later moving to front
my @meta; # place to store any found meta information (to go into META)
my $post_chomp = 0; # previous post_chomp setting
my $continue = 0; # flag for multiple directives in the same tag
my $post_op = 0; # found a post-operative DIRECTIVE
my $capture; # flag to start capture
my $func;
my $node;
pos($$str_ref) = 0 if ! $one_tag_only;
while (1) {
### continue looking for information in a semi-colon delimited tag
if ($continue) {
$node = [undef, $continue, undef];
} elsif ($one_tag_only) {
$node = [undef, pos($$str_ref), undef];
### find the next opening tag
} else {
$$str_ref =~ m{ \G (.*?) $self->{'_start_tag'} }gcxs
|| last;
my ($text, $dollar) = ($1, $2); # dollar is set only on an interpolated var
### found a text portion - chomp it and store it
if (length $text) {
if (! $post_chomp) { }
elsif ($post_chomp == 1) { $text =~ s{ ^ [^\S\n]* \n }{}x }
elsif ($post_chomp == 2) { $text =~ s{ ^ \s+ }{ }x }
elsif ($post_chomp == 3) { $text =~ s{ ^ \s+ }{}x }
push @$pointer, $text if length $text;
}
### handle variable interpolation ($2 eq $)
if ($dollar) {
### inspect previous text chunk for escape slashes
my $n = ($text =~ m{ (\\+) $ }x) ? length($1) : 0;
if ($self->{'_no_interp'} || $n % 2) { # were there odd escapes
my $prev_text;
$prev_text = \$pointer->[-1] if defined($pointer->[-1]) && ! ref($pointer->[-1]);
chop($$prev_text) if $n % 2;
if ($prev_text) { $$prev_text .= $dollar } else { push @$pointer, $dollar }
next;
}
my $not = $$str_ref =~ m{ \G ! }gcx;
my $mark = pos($$str_ref);
my $ref;
if ($$str_ref =~ m{ \G \{ }gcx) {
local $self->{'_operator_precedence'} = 0; # allow operators
$ref = $self->parse_expr($str_ref);
$$str_ref =~ m{ \G \s* $QR_COMMENTS \} }gcx
|| $self->throw('parse', 'Missing close }', undef, pos($$str_ref));
} else {
local $self->{'_operator_precedence'} = 1; # no operators
local $QR_COMMENTS = local $Template::Alloy::Parse::QR_COMMENTS = qr{};
$ref = $self->parse_expr($str_ref);
}
$self->throw('parse', "Error while parsing for interpolated string", undef, pos($$str_ref))
if ! defined $ref;
if (! $not && $self->{'SHOW_UNDEFINED_INTERP'}) {
$ref = [[undef, '//', $ref, '$'.substr($$str_ref, $mark, pos($$str_ref)-$mark)], 0];
}
push @$pointer, ['GET', $mark, pos($$str_ref), $ref];
$post_chomp = 0; # no chomping after dollar vars
next;
}
$node = [undef, pos($$str_ref), undef];
### take care of whitespace and comments flags
my $pre_chomp = $$str_ref =~ m{ \G ([+=~-]) }gcx ? $1 : $self->{'PRE_CHOMP'};
$pre_chomp =~ y/-=~+/1230/ if $pre_chomp;
if ($pre_chomp && $pointer->[-1] && ! ref $pointer->[-1]) {
if ($pre_chomp == 1) { $pointer->[-1] =~ s{ (?:\n|^) [^\S\n]* \z }{}x }
elsif ($pre_chomp == 2) { $pointer->[-1] =~ s{ (\s+) \z }{ }x }
elsif ($pre_chomp == 3) { $pointer->[-1] =~ s{ (\s+) \z }{}x }
splice(@$pointer, -1, 1, ()) if ! length $pointer->[-1]; # remove the node if it is zero length
}
### leading # means to comment the entire section
if ($$str_ref =~ m{ \G \# }gcx) {
$$str_ref =~ m{ \G (.*?) ([+~=-]?) ($self->{'_end_tag'}) }gcxs # brute force - can't comment tags with nested %]
|| $self->throw('parse', "Missing closing tag", undef, pos($$str_ref));
$node->[0] = '#';
$node->[2] = pos($$str_ref) - length($3) - length($2);
push @$pointer, $node;
$post_chomp = $2;
$post_chomp ||= $self->{'POST_CHOMP'};
$post_chomp =~ y/-=~+/1230/ if $post_chomp;
next;
}
#$$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo;
}
### look for DIRECTIVES
if ($$str_ref =~ m{ \G \s* $QR_COMMENTS $Template::Alloy::Parse::QR_DIRECTIVE }gcxo # find a word
&& ($func = $self->{'ANYCASE'} ? uc($1) : $1)
&& ($dirs->{$func}
|| ((pos($$str_ref) -= length $1) && 0))
) { # is it a directive
$$str_ref =~ m{ \G \s* $QR_COMMENTS }gcx;
$func = $aliases->{$func} if $aliases->{$func};
$node->[0] = $func;
### store out this current node level to the appropriate tree location
# on a post operator - replace the original node with the new one - store the old in the new
if ($dirs->{$func}->[3] && $post_op) {
my @post_op = @$post_op;
@$post_op = @$node;
$node = $post_op;
$node->[4] = [\@post_op];
# if there was not a semi-colon - see if semis were required
} elsif ($post_op && $self->{'SEMICOLONS'}) {
$self->throw('parse', "Missing semi-colon with SEMICOLONS => 1", undef, $node->[1]);
# handle directive captures for an item like "SET foo = BLOCK"
} elsif ($capture) {
push @{ $capture->[4] }, $node;
undef $capture;
# normal nodes
} else{
push @$pointer, $node;
}
### parse any remaining tag details
$node->[3] = eval { $dirs->{$func}->[0]->($self, $str_ref, $node) };
if (my $err = $@) {
$err->node($node) if UNIVERSAL::can($err, 'node') && ! $err->node;
die $err;
}
$node->[2] = pos $$str_ref;
### anything that behaves as a block ending
if ($func eq 'END' || $dirs->{$func}->[4]) { # [4] means it is a continuation block (ELSE, CATCH, etc)
if (! @state) {
$self->throw('parse', "Found an $func tag while not in a block", $node, pos($$str_ref));
}
my $parent_node = pop @state;
if ($func ne 'END') {
pop @$pointer; # we will store the node in the parent instead
$parent_node->[5] = $node;
my $parent_type = $parent_node->[0];
if (! $dirs->{$func}->[4]->{$parent_type}) {
$self->throw('parse', "Found unmatched nested block", $node, pos($$str_ref));
}
}
### restore the pointer up one level (because we hit the end of a block)
$pointer = (! @state) ? \@tree : $state[-1]->[4];
### normal end block
if ($func eq 'END') {
if ($parent_node->[0] eq 'BLOCK') { # move BLOCKS to front
if (defined($parent_node->[3]) && @in_view) {
push @{ $in_view[-1] }, $parent_node;
} else {
push @blocks, $parent_node
if length $parent_node->[3]; # macro blocks may not have a name
}
if ($pointer->[-1] && ! $pointer->[-1]->[6]) {
splice(@$pointer, -1, 1, ());
}
} elsif ($parent_node->[0] eq 'VIEW') {
my $ref = { map {($_->[3] => $_->[4])} @{ pop @in_view }};
unshift @{ $parent_node->[3] }, $ref;
} elsif ($dirs->{$parent_node->[0]}->[5]) { # allow no_interp to turn on and off
$self->{'_no_interp'}--;
}
### continuation block - such as an elsif
} else {
push @state, $node;
$pointer = $node->[4] ||= [];
}
### handle block directives
} elsif ($dirs->{$func}->[2] && ! $post_op) {
push @state, $node;
$pointer = $node->[4] ||= []; # allow future parsed nodes before END tag to end up in current node
push @in_view, [] if $func eq 'VIEW';
$self->{'_no_interp'}++ if $dirs->{$node->[0]}->[5] # allow no_interp to turn on and off
} elsif ($func eq 'TAGS') {
($self->{'_start_tag'}, $self->{'_end_tag'}, my $old_end) = (@{ $node->[3] }[0,1], $self->{'_end_tag'});
### allow for one more closing tag of the old style
if ($$str_ref =~ m{ \G \s* $QR_COMMENTS ([+~=-]?) $old_end }gcxs) {
$Template::Alloy::Parse::QR_COMMENTS = "(?sm: \\s*+ \\# .*? (?: \$ | (?=$self->{'_end_tag'}) ) )*+ \\s*+" if posessive();
$QR_COMMENTS = $Template::Alloy::Parse::QR_COMMENTS;
$post_chomp = $1 || $self->{'POST_CHOMP'};
$post_chomp =~ y/-=~+/1230/ if $post_chomp;
$continue = 0;
$post_op = 0;
next;
}
$Template::Alloy::Parse::QR_COMMENTS = "(?sm: \\s*+ \\# .*? (?: \$ | (?=$self->{'_end_tag'}) ) )*+ \\s*+" if posessive();
$QR_COMMENTS = $Template::Alloy::Parse::QR_COMMENTS;
} elsif ($func eq 'META') {
unshift @meta, @{ $node->[3] }; # first defined win
$node->[3] = undef; # only let these be defined once - at the front of the tree
}
### allow for bare variable getting and setting
} elsif (defined(my $var = $self->parse_expr($str_ref))) {
if ($post_op && $self->{'SEMICOLONS'}) {
$self->throw('parse', "Missing semi-colon with SEMICOLONS => 1", undef, $node->[1]);
}
push @$pointer, $node;
if ($$str_ref =~ m{ \G \s* $QR_COMMENTS ($QR_OP_ASSIGN) >? (?! [+=~-]? $self->{'_end_tag'}) \s* $QR_COMMENTS }gcx) {
$node->[0] = 'SET';
$node->[3] = eval { $dirs->{'SET'}->[0]->($self, $str_ref, $node, $1, $var) };
if (my $err = $@) {
$err->node($node) if UNIVERSAL::can($err, 'node') && ! $err->node;
die $err;
}
} else {
if ($self->{'AUTO_FILTER'}) {
$var = [[undef, '~', $var], 0] if ! ref $var;
push @$var, '|', $self->{'AUTO_FILTER'}, 0 if @$var < 3 || $var->[-3] ne '|';
}
$node->[0] = 'GET';
$node->[3] = $var;
}
$node->[2] = pos $$str_ref;
}
### look for the closing tag
if ($$str_ref =~ m{ \G \s* $QR_COMMENTS (?: ; \s* $QR_COMMENTS)? ([+=~-]?) $self->{'_end_tag'} }gcxs) {
if ($one_tag_only) {
$self->throw('parse', "Invalid char \"$1\" found at end of block") if $1;
$self->throw('parse', "Missing END directive", $state[-1], pos($$str_ref)) if @state > 0;
return \@tree;
}
$post_chomp = $1 || $self->{'POST_CHOMP'};
$post_chomp =~ y/-=~+/1230/ if $post_chomp;
$continue = 0;
$post_op = 0;
next;
}
### semi-colon = end of statement - we will need to continue parsing this tag
if ($$str_ref =~ m{ \G ; \s* $QR_COMMENTS }gcxo) {
$post_op = 0;
### we are flagged to start capturing the output of the next directive - set it up
} elsif ($node->[6]) {
$post_op = 0;
$capture = $node;
### allow next directive to be post-operative (or not)
} else {
$post_op = $node;
}
### no closing tag yet - no need to get an opening tag on next loop
$self->throw('parse', "Not sure how to handle tag", $node, pos($$str_ref)) if $continue == pos $$str_ref;
$continue = pos $$str_ref;
}
### cleanup the tree
unshift(@tree, @blocks) if @blocks;
unshift(@tree, ['META', 1, 1, \@meta]) if @meta;
$self->throw('parse', "Missing END directive", $state[-1], pos($$str_ref)) if @state > 0;
### pull off the last text portion - if any
if (pos($$str_ref) != length($$str_ref)) {
my $text = substr $$str_ref, pos($$str_ref);
if (! $post_chomp) { }
elsif ($post_chomp == 1) { $text =~ s{ ^ [^\S\n]* \n }{}x }
elsif ($post_chomp == 2) { $text =~ s{ ^ \s+ }{ }x }
elsif ($post_chomp == 3) { $text =~ s{ ^ \s+ }{}x }
push @$pointer, $text if length $text;
}
return \@tree;
}
###----------------------------------------------------------------###
sub process {
my ($self, $in, $swap, $out, @ARGS) = @_;
delete $self->{'error'};
if ($self->{'DEBUG'}) { # "enable" some types of tt style debugging
$self->{'_debug_dirs'} = 1 if $self->{'DEBUG'} =~ /^\d+$/ ? $self->{'DEBUG'} & 8 : $self->{'DEBUG'} =~ /dirs|all/;
$self->{'_debug_undef'} = 1 if $self->{'DEBUG'} =~ /^\d+$/ ? $self->{'DEBUG'} & 2 : $self->{'DEBUG'} =~ /undef|all/;
}
my $args;
$args = ($#ARGS == 0 && UNIVERSAL::isa($ARGS[0], 'HASH')) ? {%{$ARGS[0]}} : {@ARGS} if scalar @ARGS;
### get the content
my $content;
if (ref $in) {
if (ref($in) eq 'SCALAR') { # reference to a string
$content = $in;
} elsif (UNIVERSAL::isa($in, 'CODE')) {
$in = $in->();
$content = \$in;
} elsif (ref($in) eq 'HASH') { # pre-prepared document
$content = $in;
} else { # should be a file handle
local $/ = undef;
$in = <$in>;
$content = \$in;
}
} else {
### should be a filename
$content = $in;
}
### prepare block localization
my $blocks = $self->{'BLOCKS'} ||= {};
### do the swap
my $output = '';
eval {
### localize the stash
$swap ||= {};
my $var1 = $self->{'_vars'} ||= {};
my $var2 = $self->{'STASH'} || $self->{'VARIABLES'} || $self->{'PRE_DEFINE'} || {};
$var1->{'global'} ||= {}; # allow for the "global" namespace - that continues in between processing
my $copy = {%$var2, %$var1, %$swap};
local $self->{'BLOCKS'} = $blocks = {%$blocks}; # localize blocks - but save a copy to possibly restore
local $self->{'_template'};
delete $self->{'_debug_off'};
delete $self->{'_debug_format'};
### handle pre process items that go before every document
my $pre = '';
if ($self->{'PRE_PROCESS'}) {
_load_template_meta($self, $content);
foreach my $name (@{ $self->split_paths($self->{'PRE_PROCESS'}) }) {
$self->_process($name, $copy, \$pre);
}
}
### process the central file now - catching errors to allow for the ERROR config
eval {
local $self->{'STREAM'} = undef if $self->{'WRAPPER'};
### handle the PROCESS config - which loads another template in place of the real one
if (exists $self->{'PROCESS'}) {
_load_template_meta($self, $content);
foreach my $name (@{ $self->split_paths($self->{'PROCESS'}) }) {
next if ! length $name;
$self->_process($name, $copy, \$output);
}
### handle "normal" content
} else {
local $self->{'_start_top_level'} = 1;
$self->_process($content, $copy, \$output);
}
};
### catch errors with ERROR config
if (my $err = $@) {
$err = $self->exception('undef', $err) if ! UNIVERSAL::can($err, 'type');
die $err if $err->type =~ /stop|return/;
my $catch = $self->{'ERRORS'} || $self->{'ERROR'} || die $err;
$catch = {default => $catch} if ! ref $catch;
my $type = $err->type;
my $last_found;
my $file;
foreach my $name (keys %$catch) {
my $_name = (! defined $name || lc($name) eq 'default') ? '' : $name;
if ($type =~ / ^ \Q$_name\E \b /x
&& (! defined($last_found) || length($last_found) < length($_name))) { # more specific wins
$last_found = $_name;
$file = $catch->{$name};
}
}
### found error handler - try it out
if (defined $file) {
$output = '';
local $copy->{'error'} = local $copy->{'e'} = $self->{'COMPILE_JS'} ? {type => $type, info => $err->info} : $err;
local $self->{'STREAM'} = undef if $self->{'WRAPPER'};
$self->_process($file, $copy, \$output);
}
}
### handle wrapper directives
if (exists $self->{'WRAPPER'}) {
_load_template_meta($self, $content);
foreach my $name (reverse @{ $self->split_paths($self->{'WRAPPER'}) }) {
next if ! length $name;
local $copy->{'content'} = $output;
my $out = '';
local $self->{'STREAM'} = undef;
$self->_process($name, $copy, \$out);
$output = $out;
}
if ($self->{'STREAM'}) {
print $output;
$output = 1;
}
}
$output = $pre . $output if length $pre;
### handle post process items that go after every document
if ($self->{'POST_PROCESS'}) {
_load_template_meta($self, $content);
foreach my $name (@{ $self->split_paths($self->{'POST_PROCESS'}) }) {
$self->_process($name, $copy, \$output);
}
}
};
### clear blocks as asked (AUTO_RESET) defaults to on
$self->{'BLOCKS'} = $blocks if exists($self->{'AUTO_RESET'}) && ! $self->{'AUTO_RESET'};
if (my $err = $@) {
$err = $self->exception('undef', $err) if ! UNIVERSAL::can($err, 'type');
if ($err->type !~ /stop|return|next|last|break/) {
$self->{'error'} = $err;
die $err if $self->{'RAISE_ERROR'};
return;
}
}
### send the content back out
$out ||= $self->{'OUTPUT'};
if (ref $out) {
if (UNIVERSAL::isa($out, 'CODE')) {
$out->($output);
} elsif (UNIVERSAL::can($out, 'print')) {
$out->print($output);
} elsif (UNIVERSAL::isa($out, 'SCALAR')) { # reference to a string
$$out = $output;
} elsif (UNIVERSAL::isa($out, 'ARRAY')) {
push @$out, $output;
} else { # should be a file handle
print {$out} $output;
}
} elsif ($out) { # should be a filename
my $file;
if ($out =~ m|^/|) {
if (! $self->{'ABSOLUTE'}) {
$self->throw($self->{'error'} = $self->exception('file', "ABSOLUTE paths disabled"));
} else {
$file = $out;
}
} elsif ($out =~ m|^\.\.?/|) {
if (! $self->{'RELATIVE'}) {
$self->throw($self->{'error'} = $self->exception('file', "RELATIVE paths disabled"));
} else {
$file = $out;
}
} else {
my $path = $self->{'OUTPUT_PATH'};
$path = '.' if ! defined $path;
if (! -d $path) {
require File::Path;
File::Path::mkpath($path);
}
$file = "$path/$out";
}
open(my $fh, '>', $file)
|| $self->throw($self->{'error'} = $self->exception('file', "$out couldn't be opened for writing: $!"));
if (my $bm = $args->{'binmode'}) {
if (+$bm == 1) { binmode $fh }
else { binmode $fh, $bm }
} elsif ($self->{'ENCODING'}) {
if (eval { require Encode } && defined &Encode::encode) {
$output = Encode::encode($self->{'ENCODING'}, $output);
}
}
print {$fh} $output;
} else {
print $output;
}
return if $self->{'error'};
return 1;
}
sub _load_template_meta {
my $self = shift;
return if $self->{'_template'}; # only do once as need
eval {
### load the meta data for the top document
### this is needed by some of the custom handlers such as PRE_PROCESS and POST_PROCESS
my $content = shift;
my $doc = $self->{'_template'} = ref($content) eq 'HASH' ? $content : $self->load_template($content) || {};
my $meta = $doc->{'_perl'} ? $doc->{'_perl'}->{'meta'}
: ($doc->{'_tree'} && ref($doc->{'_tree'}->[0]) && $doc->{'_tree'}->[0]->[0] eq 'META') ? $doc->{'_tree'}->[0]->[3]
: {};
$meta = {@$meta} if ref($meta) eq 'ARRAY';
$self->{'_template'} = $doc;
@{ $doc }{keys %$meta} = values %$meta;
};
return;
}
###----------------------------------------------------------------###
1;
__END__
=head1 DESCRIPTION
The Template::Alloy::TT role provides the syntax and the interface for
Template::Toolkit version 1, 2, and 3. It also brings many of the
features from the various templating systems.
And it is fast.
See the Template::Alloy documentation for configuration and other
parameters.
=head1 HOW IS Template::Alloy DIFFERENT FROM Template::Toolkit
Alloy uses the same base template syntax and configuration items as
TT2, but the internals of Alloy were written from scratch.
Additionally much of the planned TT3 syntax is supported as well as
most of that of HTML::Template::Expr. The following is a list of some
of the ways that the configuration and syntax of Alloy are different
from that of TT2. Note: items that are planned to work in TT3 are
marked with (TT3).
=over 4
=item
Numerical hash keys work
[% a = {1 => 2} %]
=item
Quoted hash key interpolation is fine
[% a = {"$foo" => 1} %]
=item
Multiple ranges in same constructor
[% a = [1..10, 21..30] %]
=item
Constructor types can call virtual methods. (TT3)
[% a = [1..10].reverse %]
[% "$foo".length %]
[% 123.length %] # = 3
[% 123.4.length %] # = 5
[% -123.4.length %] # = -5 ("." binds more tightly than "-")
[% (a ~ b).length %]
[% "hi".repeat(3) %] # = hihihi
[% {a => b}.size %] # = 1
=item
The "${" and "}" variable interpolators can contain expressions,
not just variables.
[% [0..10].${ 1 + 2 } %] # = 4
[% {ab => 'AB'}.${ 'a' ~ 'b' } %] # = AB
[% color = qw/Red Blue/; FOR [1..4] ; color.${ loop.index % color.size } ; END %]
# = RedBlueRedBlue
=item
You can use regular expression quoting.
[% "foo".match( /(F\w+)/i ).0 %] # = foo
=item
Tags can be nested.
[% f = "[% (1 + 2) %]" %][% f|eval %] # = 3
=item
Arrays can be accessed with non-integer numbers.
[% [0..10].${ 2.3 } %] # = 3
=item
Reserved names are less reserved. (TT3)
[% GET GET %] # gets the variable named "GET"
[% GET $GET %] # gets the variable who's name is stored in "GET"
=item
Filters and SCALAR_OPS are interchangeable. (TT3)
[% a | length %]
[% b . lower %]
=item
Pipe "|" can be used anywhere dot "." can be and means to call
the virtual method. (TT3)
[% a = {size => "foo"} %][% a.size %] # = foo
[% a = {size => "foo"} %][% a|size %] # = 1 (size of hash)
=item
Pipe "|" and "." can be mixed. (TT3)
[% "aa" | repeat(2) . length %] # = 4
=item
Added V2PIPE configuration item
Restores the behavior of the pipe operator to be
compatible with TT2.
With V2PIPE = 1
[% PROCESS a | repeat(2) %] # = value of block or file a repeated twice
With V2PIPE = 0 (default)
[% PROCESS a | repeat(2) %] # = process block or file named a ~ a
=item
Added V2EQUALS configuration item
Allows for turning off TT2 "==" behavior. Defaults to 1
in TT syntaxes and to 0 in HT syntaxes.
[% CONFIG V2EQUALS => 1 %][% ('7' == '7.0') || 0 %]
[% CONFIG V2EQUALS => 0 %][% ('7' == '7.0') || 0 %]
Prints
0
1
=item
Added AUTO_EVAL configuration item.
Default false. If true, will automatically call eval filter
on double quoted strings.
=item
Added SHOW_UNDEFINED_INTERP configuration item.
Default false. If true, will leave in place interpolated
values that weren't defined. You can then use the
Velocity notation $!foo to not show these values.
=item
Added Virtual Object Namespaces. (TT3)
The Text, List, and Hash types give direct access
to virtual methods.
[% a = "foobar" %][% Text.length(a) %] # = 6
[% a = [1 .. 10] %][% List.size(a) %] # = 10
[% a = {a=>"A", b=>"B"} ; Hash.size(a) %] = 2
[% foo = {a => 1, b => 2}
| Hash.keys
| List.join(", ") %] # = a, b
=item
Added "fmt" scalar, list, and hash virtual methods.
[% list.fmt("%s", ", ") %]
[% hash.fmt("%s => %s", "\n") %]
=item
Added missing HTML::Template::Expr vmethods
The following vmethods were added - they correspond to the
perl functions of the same name.
abs
atan2
cos
exp
hex
lc
log
oct
sin
sprintf
sqrt
srand
uc
=item
Allow all Scalar vmethods to behave as top level functions.
[% sprintf("%d %d", 7, 8) %] # = "7 8"
The following are equivalent in Alloy:
[% "abc".length %]
[% length("abc") %]
This feature may be disabling by setting the
VMETHOD_FUNCTIONS configuration item to 0.
This is similar to how HTML::Template::Expr operates, but
now you can use this functionality in TT templates as well.
=item
Whitespace is less meaningful. (TT3)
[% 2-1 %] # = 1 (fails in TT2)
=item
Added pow operator.
[% 2 ** 3 %] [% 2 pow 3 %] # = 8 8
=item
Added string comparison operators (gt ge lt le cmp)
[% IF "a" lt "b" %]a is less[% END %]
=item
Added numeric comparison operator (<=>)
This can be used to make up for the fact that TT2 made == the
same as eq (which will hopefully change - use eq when you mean eq).
[% IF ! (a <=> b) %]a == b[% END %]
[% IF (a <=> b) %]a != b[% END %]
=item
Added self modifiers (+=, -=, *=, /=, %=, **=, ~=). (TT3)
[% a = 2; a *= 3 ; a %] # = 6
[% a = 2; (a *= 3) ; a %] # = 66
=item
Added pre and post increment and decrement (++ --). (TT3)
[% ++a ; ++a %] # = 12
[% a-- ; a-- %] # = 0-1
=item
Added qw// contructor. (TT3)
[% a = qw(a b c); a.1 %] # = b
[% qw/a b c/.2 %] # = c
=item
Added regex contructor. (TT3)
[% "FOO".match(/(foo)/i).0 %] # = FOO
[% a = /(foo)/i; "FOO".match(a).0 %] # = FOO
=item
Allow for scientific notation. (TT3)
[% a = 1.2e-20 %]
[% 123.fmt('%.3e') %] # = 1.230e+02
=item
Allow for hexadecimal input. (TT3)
[% a = 0xff0000 %][% a %] # = 16711680
[% a = 0xff2 / 0xd; a.fmt('%x') %] # = 13a
=item
FOREACH variables can be nested.
[% FOREACH f.b = [1..10] ; f.b ; END %]
Note that nested variables are subject to scoping issues.
f.b will not be reset to its value before the FOREACH.
=item
Post operative directives can be nested. (TT3)
Andy Wardley calls this side-by-side effect notation.
[% one IF two IF three %]
same as
[% IF three %][% IF two %][% one %][% END %][% END %]
[% a = [[1..3], [5..7]] %][% i FOREACH i = j FOREACH j = a %] # = 123567
=item
Semi-colons on directives in the same tag are optional. (TT3)
[% SET a = 1
GET a
%]
[% FOREACH i = [1 .. 10]
i
END %]
Note: a semi-colon is still required in front of any block directive
that can be used as a post-operative directive.
[% 1 IF 0
2 %] # prints 2
[% 1; IF 0
2
END %] # prints 1
Note2: This behavior can be disabled by setting the SEMICOLONS
configuration item to a true value. If SEMICOLONS is true, then
a SEMICOLON must be set after any directive that isn't followed
by a post-operative directive.
=item
CATCH blocks can be empty.
TT2 requires them to contain something.
=item
Added a DUMP directive.
Used for Data::Dumper'ing the passed variable or expression.
[% DUMP a.a %]
=item
Added CONFIG directive.
[% CONFIG
ANYCASE => 1
PRE_CHOMP => '-'
%]
=item
Configuration options can use lowercase names instead
of the all uppercase names that TT2 uses.
my $t = Template::Alloy->new({
anycase => 1,
interpolate => 1,
});
=item
Added LOOP directive (works the same as LOOP in HTML::Template.
[%- var = [{key => 'a'}, {key => 'b'}] %]
[%- LOOP var %]
([% key %])
[%- END %]
Prints
(a)
(b)
=item
Alloy can parse HTML::Template and HTML::Template::Expr documents
as well as TT2 and TT3 documents.
=item
Added SYNTAX configuration. The SYNTAX configuration can be
used to change what template syntax will be used for parsing
included templates or eval'ed strings.
[% CONFIG SYNTAX => 'hte' %]
[% var = '<TMPL_VAR EXPR="sprintf('%s', 'hello world')">' %]
[% var | eval %]
=item
Added @() and $() and CALL_CONTEXT. Template::Toolkit uses a
\concept that Alloy refers to as "smart" context. All function
calls or method calls of variables in Template::Toolkit are made
in list context. If one item is in the list, it is returned. If
two or more items are returned - it returns an arrayref. This
"does the right thing" most of the time - but can cause confusion
in some cases and is difficult to work around without writing
wrappers for the functions or methods in Perl.
Alloy has introduced the CALL_CONTEXT configuration item which
defaults to "smart," but can also be set to "list" or "item."
List context will always return an arrayref from called functions
and methods and will call in list context. Item context will
always call in item (scalar) context and will return one item.
The @() and $() operators allow for functions embedded inside
to use list and item context (respectively). They are modeled
after the corresponding Perl 6 context specifiers. See the
Template::Alloy::Operators perldoc and CALL_CONTEXT configuration
documentation for more information.
[% array = @( this.get_rows ) %]
[% item = $( this.get_something ) %]
=item
Added -E<gt>() MACRO operator.
The -E<gt>() operator behaves similarly to the MACRO directive,
but can be used to pass functions to map, grep, and sort vmethods.
[% MACRO foo(n) BLOCK %]Say [% n %][% END %]
[% foo = ->(n){ "Say $n" } %]
[% [0..10].grep(->(this % 2)).join %] prints 3 5 7 9
[% ['a' .. 'c'].map(->(a){ a.upper }).join %] prints A B C
[% [1,2,3].sort(->(a,b){ b <=> a }).join %] prints 3 2 1
=item
The RETURN directive can take a variable or expression as a return
value. Their are also "return" list, item, and hash vmethods. Return
will also return from an enclosing MACRO.
[% a = ->(n){ [1..n].return } %]
=item
Alloy does not generate Perl code.
It generates an "opcode" tree. The opcode tree is an arrayref
of scalars and array refs nested as deeply as possible. This "simple"
structure could be shared TT implementations in other languages
via JSON or YAML. You can optionally enable generating Perl code by
setting COMPILE_PERL = 1.
=item
Alloy uses storable for its compiled templates.
If EVAL_PERL is off, Alloy will not eval_string on ANY piece of information.
=item
There is eval_filter and MACRO recursion protection
You can control the nested nature of eval_filter and MACRO
recursion using the MAX_EVAL_RECURSE and MAX_MACRO_RECURSE
configuration items.
=item
There is no context.
Alloy provides a context object that mimics the Template::Context
interface for use by some TT filters, eval perl blocks, views,
and plugins.
=item
There is no provider.
Alloy uses the load_template method to get and cache templates.
=item
There is no parser/grammar.
Alloy has its own built-in recursive regex based parser and grammar system.
Alloy can actually be substituted in place of the native Template::Parser and
Template::Grammar in TT by using the Template::Parser::Alloy module. This
module uses the output of parse_tree to generate a TT style compiled perl
document.
=item
The DEBUG directive is more limited.
It only understands DEBUG_DIRS (8) and DEBUG_UNDEF (2).
=item
Alloy has better line information
When debug dirs is on, directives on different lines separated
by colons show the line they are on rather than a general line range.
Parse errors actually know what line and character they occurred at.
=back
=head1 UNSUPPORTED TT2 CONFIGURATION
=over 4
=item LOAD_TEMPLATES
Template::Alloy has its own mechanism for loading and storing compiled
templates. TT would use a Template::Provider that would return a
Template::Document. The closest thing in Template::Alloy is the
load_template method. There is no immediate plan to support the TT
behavior.
=item LOAD_PLUGINS
Template::Alloy uses its own mechanism for loading plugins. TT would
use a Template::Plugins object to load plugins requested via the USE
directive. The functionality for doing this in Template::Alloy is
contained in the list_plugins method and the play_USE method. There
is no immediate plan to support the TT behavior.
Full support is offered for the PLUGINS and LOAD_PERL configuration
items.
Also note that Template::Alloy only has native support for the Iterator
plugin. Any of the other plugins requested will need to provided by
installing Template::Toolkit or the appropriate plugin module.
=item LOAD_FILTERS
Template::Alloy uses its own mechanism for loading filters. TT would
use the Template::Filters object to load filters requested via the
FILTER directive. The functionality for doing this in Template::Alloy
is contained in the list_filters method and the play_expr method.
Full support is offered for the FILTERS configuration item.
=item TOLERANT
This option is used by the LOAD_TEMPLATES and LOAD_PLUGINS options and
is not applicable in Template::Alloy.
=item SERVICE
Template::Alloy has no concept of service (theoretically the
Template::Alloy is the "service").
=item CONTEXT
Template::Alloy provides its own pseudo context object to plugins,
filters, and perl blocks. The Template::Alloy model doesn't really
allow for a separate context. Template::Alloy IS the context.
=item PARSER
Template::Alloy has its own built in parser. The closest similarity
is the parse_tree method. The output of parse_tree is an optree that
is later run by execute_tree. Alloy provides a backend to the
Template::Parser::Alloy module which can be used to replace the
default parser when using the standard Template::Toolkit library.
=item GRAMMAR
Template::Alloy maintains its own grammar. The grammar is defined
in the parse_tree method and the callbacks listed in the global
$Template::Alloy::Parse::DIRECTIVES hashref.
=back
=head1 AUTHOR
Paul Seamons <paul@seamons.com>
=head1 LICENSE
This module may be distributed under the same terms as Perl itself.
=cut
|