]> git.imager.perl.org - bse.git/commitdiff
for dynamic generation, replace code with not set vars with an error
authorTony Cook <tony@develop-help.com>
Thu, 28 Aug 2014 04:36:46 +0000 (14:36 +1000)
committerTony Cook <tony@develop-help.com>
Fri, 19 Sep 2014 06:04:48 +0000 (16:04 +1000)
this can be restored to the old behaviour with

  [basic]
  error_not_defined=0

site/cgi-bin/modules/BSE/Template.pm
site/cgi-bin/modules/Squirrel/Template.pm
site/cgi-bin/modules/Squirrel/Template/Processor.pm
site/docs/config.pod
t/020-templater/040-original.t
t/050-local/050-dyncat.t

index 2868f37e0ddbe8a235dcfad3d28427eacc2bf8ed..f7bc8b44a38649d9467535ffee1ecfac9122b412 100644 (file)
@@ -4,7 +4,7 @@ use Squirrel::Template;
 use Carp qw(confess cluck);
 use Config ();
 
-our $VERSION = "1.012";
+our $VERSION = "1.013";
 
 my %formats =
   (
@@ -62,6 +62,8 @@ sub templater {
        [ "<:", ":>" ],
        [ "[:", ":]" ],
       ];
+    $topts{error_not_defined} =
+      $cfg->entry("basic", "error_not_defined", 1);
   }
 
   return Squirrel::Template->new(%topts);
index d91bb59c54a62c2ea6cedc8bbb4e6d1dc9714aa0..02acd388b68644611cd418d2a3081f9420447026 100644 (file)
@@ -20,7 +20,7 @@ BEGIN {
 
 use constant DEBUG_GET_PARMS => 0;
 
-our $VERSION = "1.030";
+our $VERSION = "1.031";
 
 my %compile_cache;
 
@@ -388,7 +388,10 @@ sub get_var {
     }
   }
 
-  die "ENOIMPL\nVariable $name not defined";
+  $self->{error_not_defined}
+    and die "Variable '$name' not set\n";
+
+  die "ENOIMPL\nVariable '$name' not set";
 }
 
 sub set_var {
index be664b0b1e6ac0eb75c64cbcf7a145605ed6f193..30de50bedd5db6b298aeebed1dd24160141256e0 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use Squirrel::Template::Constants qw(:node);
 use Scalar::Util ();
 
-our $VERSION = "1.025";
+our $VERSION = "1.026";
 
 use constant ACTS => 0;
 use constant TMPLT => 1;
@@ -81,7 +81,11 @@ sub _process_expr {
 
   my @errors;
   my $value = "";
-  unless (eval { $value = $self->[EVAL]->process($node->[NODE_EXPR_EXPR]); 1 }) {
+  unless (eval {
+    local $SIG{__DIE__};
+    $value = $self->[EVAL]->process($node->[NODE_EXPR_EXPR]);
+    1
+  }) {
     my $msg = $@;
 
     if ($msg =~ /\bENOIMPL\b/) {
@@ -104,7 +108,11 @@ sub _process_set {
 
   my @errors;
   my $value = "";
-  if (eval { $value = $self->[EVAL]->process($node->[NODE_SET_EXPR]); 1 }) {
+  if (eval {
+    local $SIG{__DIE__};
+    $value = $self->[EVAL]->process($node->[NODE_SET_EXPR]); 
+    1
+  }) {
     my @var = @{$node->[NODE_SET_VAR]};
     if (@var > 1) {
       my $top_name = shift @var;
@@ -172,6 +180,7 @@ sub _process_call {
   my $defaults;
   my $name;
   if (eval {
+    local $SIG{__DIE__};
     $name = $self->[EVAL]->process($node->[NODE_CALL_NAME]);
     for my $arg (@{$node->[NODE_CALL_LIST]}) {
       my $key = $self->[EVAL]->process($arg->[0]);
@@ -224,6 +233,7 @@ sub _process_for {
 
   my $list;
   unless (eval {
+    local $SIG{__DIE__};
     $list = $self->[EVAL]->process($node->[NODE_FOR_EXPR]); 1;
   }) {
     my $msg = $@;
@@ -523,6 +533,7 @@ sub _process_ext_wrap {
   my @errors;
   my $name;
   if (eval {
+    local $SIG{__DIE__};
     $name = $self->[EVAL]->process($filename);
     for my $arg (@$args) {
       my $key = $self->[EVAL]->process($arg->[0]);
@@ -637,7 +648,11 @@ sub _process_ext_if {
   my @conds = @{$node->[NODE_EXTIF_CONDS]};
   while (my $cond = shift @conds) {
     my $result;
-    if (eval { $result = $self->[EVAL]->process($cond->[2]); 1 }) {
+    if (eval {
+      local $SIG{__DIE__};
+      $result = $self->[EVAL]->process($cond->[2]);
+      1
+    }) {
       if ($result) {
        return $self->process($cond->[1]);
       }
@@ -704,6 +719,7 @@ sub _process_iterateover {
   my $call;
   my @args;
   unless (eval {
+    local $SIG{__DIE__};
     $call = $self->[EVAL]->process($node->[NODE_ITERATEOVER_CALL]);
     @args = map $self->[EVAL]->process($_), @{$node->[NODE_ITERATEOVER_ARGS]};
     1;
@@ -751,7 +767,11 @@ sub _process_while {
 
   my $cond = $node->[NODE_WHILE_COND];
   my $result;
-  unless (eval { $result = $self->[EVAL]->process($cond); 1 }) {
+  unless (eval {
+    local $SIG{__DIE__};
+    $result = $self->[EVAL]->process($cond);
+    1
+  }) {
     my $msg = $@;
     if (!ref $msg && $msg =~ /\bENOIMPL\b/) {
       return
@@ -769,7 +789,11 @@ sub _process_while {
   while ($result) {
     push @output, $self->process($node->[NODE_WHILE_CONTENT]);
 
-    unless (eval { $result = $self->[EVAL]->process($cond); 1 }) {
+    unless (eval {
+      local $SIG{__DIE__};
+      $result = $self->[EVAL]->process($cond);
+      1
+    }) {
       my $msg = $@;
       if (!ref $msg && $msg ==~ /\bENOIMPL\b/) {
        return
index 4e0a61c32af262d17cf3a057bce1704050462ad5..6a4f1186db219df807176adab9e77e4c8389946e 100644 (file)
@@ -358,6 +358,12 @@ gpopimage[] tags.  Default: popup.
 If set to 0, dynamic article iterators will no access control filter
 their results.  Default: 1.
 
+=item error_not_defined
+
+If non-zero, during dynamic page generation, inserts a message
+explaining which variable is not set, instead of just leaving the tag
+unreplaced.  Default: 1.
+
 =item http_only_session
 
 If this is non-zero, the default, the session cookie sent to the
index 07c58e290c41915e519d574292e52abbc7a30101..c8bcea28b2a8742bf9c2ab605433d09572656d0d 100644 (file)
@@ -1,13 +1,15 @@
 #!perl -w
 # Basic tests for Squirrel::Template
 use strict;
-use Test::More tests => 198;
+use Test::More tests => 201;
 use HTML::Entities;
 
 sub template_test($$$$;$$);
 
 my $gotmodule = require_ok('Squirrel::Template');
 
+my %extra_opts;
+
 SKIP: {
   skip "couldn't load module", 15 unless $gotmodule;
 
@@ -817,6 +819,32 @@ FOO
 
 a,b
 OUT
+
+  {
+    local $extra_opts{error_not_defined} = 1;
+    template_test(<<'IN', <<'OUT', "error !defined: expr", \%acts, "", \%vars);
+<:= unknown_var :>
+IN
+* Variable 'unknown_var' not set
+ *
+OUT
+    template_test(<<'IN', <<'OUT', "error !defined: .if", \%acts, "", \%vars);
+<:.if unknown_var -:>
+a
+<:-.else -:>
+b
+<:-.end if:>
+IN
+* Variable 'unknown_var' not set
+ *
+OUT
+    template_test(<<'IN', <<'OUT', "error !defined: .set", \%acts, "", \%vars);
+<:.set foo = unknown_var :>
+IN
+* Variable 'unknown_var' not set
+ *
+OUT
+  }
 }
 
 sub template_test ($$$$;$$) {
@@ -835,7 +863,8 @@ sub template_test ($$$$;$$) {
       html => sub {
        encode_entities($_[0], '&<>');
       }
-     }
+     },
+     %extra_opts,
     );
 
   my $result = $templater->replace_template($in, $acts, undef, "test", $vars);
index aa621b56e12ef945840444cbc2f5d8f9996a19e0..11cfa8ccb54bc32c5af3ac32b48966c244959c40 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use BSE::Test ();
-use Test::More tests => 98;
+use Test::More tests => 101;
 use File::Spec;
 use FindBin;
 my $cgidir = File::Spec->catdir(BSE::Test::base_dir, 'cgi-bin');
@@ -515,6 +515,13 @@ Embedded: [0]
 Dynamic: [1]
 EXPECTED
 
+dyn_template_test "unknown vars", $parent, <<TEMPLATE, <<EXPECTED;
+<:= unknown_var :>
+TEMPLATE
+* Variable 'unknown_var' not set
+ *
+EXPECTED
+
 $prod4->remove($cfg);
 for my $prod (@prods) {
   $prod->remove($cfg);