Since nobody bothered answering my question on how best to implement
this option, I decided to add to the long list of flags than the short
list of tags :)
The changes were relatively simple, and seems to work just fine with my
site. Comments on the implementation of the patch welcome.
Tugrul Galatali
--- ORIG/Template.pm Fri Jun 21 19:06:27 2002
+++ Template.pm Fri Jun 21 19:02:29 2002
@@ -927,6 +927,7 @@
no_includes => 0,
case_sensitive => 0,
filter => [],
+ ifdefined => 0,
);
# load in options supplied to new()
@@ -2516,10 +2517,14 @@
if ($line->[HTML::Template::COND::JUMP_IF_TRUE]) {
if ($line->[HTML::Template::COND::VARIABLE_TYPE] ==
HTML::Template::COND::VARIABLE_TYPE_VAR) {
if (defined ${$line->[HTML::Template::COND::VARIABLE]}) {
- if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') {
- $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if
${$line->[HTML::Template::COND::VARIABLE]}->($self);
+ if ($options->{ifdefined}) {
+ $x = $line->[HTML::Template::COND::JUMP_ADDRESS];
} else {
- $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if
${$line->[HTML::Template::COND::VARIABLE]};
+ if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') {
+ $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if
+${$line->[HTML::Template::COND::VARIABLE]}->($self);
+ } else {
+ $x = $line->[HTML::Template::COND::JUMP_ADDRESS] if
+${$line->[HTML::Template::COND::VARIABLE]};
+ }
}
}
} else {
@@ -2530,10 +2535,12 @@
} else {
if ($line->[HTML::Template::COND::VARIABLE_TYPE] ==
HTML::Template::COND::VARIABLE_TYPE_VAR) {
if (defined ${$line->[HTML::Template::COND::VARIABLE]}) {
- if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') {
- $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless
${$line->[HTML::Template::COND::VARIABLE]}->($self);
- } else {
- $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless
${$line->[HTML::Template::COND::VARIABLE]};
+ if (!$options->{ifdefined}) {
+ if (ref(${$line->[HTML::Template::COND::VARIABLE]}) eq 'CODE') {
+ $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless
+${$line->[HTML::Template::COND::VARIABLE]}->($self);
+ } else {
+ $x = $line->[HTML::Template::COND::JUMP_ADDRESS] unless
+${$line->[HTML::Template::COND::VARIABLE]};
+ }
}
} else {
$x = $line->[HTML::Template::COND::JUMP_ADDRESS];
--- ORIG/JIT.pm Fri Jun 21 19:06:42 2002
+++ JIT.pm Fri Jun 21 19:12:42 2002
@@ -39,6 +39,7 @@
$args{global_vars} ||= 0;
$args{print_to_stdout} ||= 0;
$args{case_sensitive} ||= 0;
+ $args{ifdefined} ||= 0;
# get a hash of the path and mtime. hashing them together means
# that everytime the template file is changed we'll get a new md5
@@ -46,7 +47,8 @@
my $path_md5 = md5_hex($path . (stat($path))[9] . $VERSION .
join(' ', $args{global_vars},
$args{print_to_stdout},
- $args{case_sensitive}));
+ $args{case_sensitive},
+ $args{ifdefined}));
# compute package and filesystem details
my $package = "tmpl_$path_md5"; # package name
--- ORIG/Compiler.pm Fri Jun 21 19:07:49 2002
+++ Compiler.pm Fri Jun 21 19:18:16 2002
@@ -31,6 +31,7 @@
$self->{global_vars} = $args{global_vars};
$self->{print_to_stdout} = $args{print_to_stdout};
$self->{case_sensitive} = $args{case_sensitive};
+ $self->{ifdefined} = $args{ifdefined};
# compile internal representation into a chunk of C code
@@ -279,12 +280,22 @@
my @code;
if ($is_var) {
- if ($is_unless) {
- # unless var
- push(@code, "if (!SvTRUE($var)) {");
+ if ($self->{ifdefined}) {
+ if ($is_unless) {
+ # unless var
+ push(@code, "if ($var == &PL_sv_undef) {");
+ } else {
+ # if var
+ push(@code, "if ($var != &PL_sv_undef) {");
+ }
} else {
- # if var
- push(@code, "if (SvTRUE($var)) {");
+ if ($is_unless) {
+ # unless var
+ push(@code, "if (!SvTRUE($var)) {");
+ } else {
+ # if var
+ push(@code, "if (SvTRUE($var)) {");
+ }
}
} else {
if ($is_unless) {