]> git.imager.perl.org - bse.git/commitdiff
template re-work: add a preload template to allow for common definitions
authorTony Cook <tony@develop-help.com>
Fri, 20 Apr 2012 01:02:34 +0000 (11:02 +1000)
committerTony Cook <tony@develop-help.com>
Fri, 20 Apr 2012 01:06:33 +0000 (11:06 +1000)
MANIFEST
site/cgi-bin/modules/BSE/Template.pm
site/cgi-bin/modules/Squirrel/Template.pm
site/docs/config.pod
t/t010template.t
t/templates/preload.tmpl [new file with mode: 0644]

index 6e15618a604a960144b03baef0391a5d679e1fba..4a425b6fa419a9f2843d2b46cb4c662c8dbd79a5 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -865,6 +865,7 @@ t/templater/30expr.t
 t/templates/called.tmpl
 t/templates/included.include   Used by t010template.t
 t/templates/included.recursive
 t/templates/called.tmpl
 t/templates/included.include   Used by t010template.t
 t/templates/included.recursive
+t/templates/preload.tmpl
 t/templates/wrapinner.tmpl
 t/templates/wrapself.tmpl
 t/templates/wraptest.tmpl      Used by t010template.t
 t/templates/wrapinner.tmpl
 t/templates/wrapself.tmpl
 t/templates/wraptest.tmpl      Used by t010template.t
index 003da8bf195fe53914a04f668b72b3936df819ab..113e0f29f8abbcf8b48201ee75a6fcc338dc5209 100644 (file)
@@ -43,6 +43,8 @@ sub templater {
     $opts{cache} = BSE::Cache->load($cfg);
   }
 
     $opts{cache} = BSE::Cache->load($cfg);
   }
 
+  $opts{preload} = $cfg->entry("basic", "preload_template");
+
   return Squirrel::Template->new(%opts);
 }
 
   return Squirrel::Template->new(%opts);
 }
 
index 5430e7a0026a60cbcef55bb8c051d0eb53c98550..87a1a1e98ba0810dfcc516b59b89220b94362091 100644 (file)
@@ -19,7 +19,7 @@ BEGIN {
 
 use constant DEBUG_GET_PARMS => 0;
 
 
 use constant DEBUG_GET_PARMS => 0;
 
-our $VERSION = "1.018";
+our $VERSION = "1.019";
 
 my $tag_head = qr/(?:\s+<:-|<:-?)/;
 my $tag_tail = qr/(?:-:>\s*|:>)/;
 
 my $tag_head = qr/(?:\s+<:-|<:-?)/;
 my $tag_tail = qr/(?:-:>\s*|:>)/;
@@ -486,6 +486,17 @@ sub replace {
 
   my $processor = Squirrel::Template::Processor->new($acts, $self);
 
 
   my $processor = Squirrel::Template::Processor->new($acts, $self);
 
+  if ($self->{preload}) {
+    my ($parsed, $error) = $self->parse_file($self->{preload});
+    if ($parsed) {
+      # process and discard, just for definitions, initializations
+      $processor->process($parsed);
+    }
+    elsif ($error) {
+      push @{$self->{errors}}, [ error => "", 0, $self->{preload}, $error ];
+    }
+  }
+
   return wantarray
     ? $processor->process($parsed)
       : join("", $processor->process($parsed));
   return wantarray
     ? $processor->process($parsed)
       : join("", $processor->process($parsed));
index 4dccfe3a6e0eab0f100870b42c0ef123f07c8d5c..19bb60622a87407937242d90f2830b10cec66266 100644 (file)
@@ -444,6 +444,13 @@ If true, BSE will cache compiled templates using the configured BSE
 cache, if any.  Depending on the configured cache this may slow things
 down.  Default: disabled.
 
 cache, if any.  Depending on the configured cache this may slow things
 down.  Default: disabled.
 
+=item preload_template
+
+Preload the named template, searching the template search part.  Any
+text that would normally be produced by the preloaded template is
+ignored.  This can be useful for common definitions and variables for
+use in many templates.  Default: none.
+
 =item auto_images
 
 By default, if the author doesn't use any image tags, BSE will insert
 =item auto_images
 
 By default, if the author doesn't use any image tags, BSE will insert
index e200115b5af4f6efa855677d4fe50a2afa95e3d7..89355163f10f65fc7a991baa724f648d76d6e6ba 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 # Basic tests for Squirrel::Template
 use strict;
 #!perl -w
 # Basic tests for Squirrel::Template
 use strict;
-use Test::More tests => 106;
+use Test::More tests => 107;
 
 sub template_test($$$$;$$);
 
 
 sub template_test($$$$;$$);
 
@@ -468,6 +468,9 @@ IN
 IN
   template_test(<<IN, "other value", "external call", \%acts, "", \%vars);
 <:.call "called.tmpl", "avar":"other value"-:>
 IN
   template_test(<<IN, "other value", "external call", \%acts, "", \%vars);
 <:.call "called.tmpl", "avar":"other value"-:>
+IN
+  template_test(<<IN, "This was preloaded", "call preloaded", \%acts, "both", \%vars);
+<:.call "preloaded"-:>
 IN
   template_test(<<IN, <<OUT, "simple .for", \%acts, "", \%vars);
 <:.for x in [ "a" .. "d" ] -:>
 IN
   template_test(<<IN, <<OUT, "simple .for", \%acts, "", \%vars);
 <:.for x in [ "a" .. "d" ] -:>
@@ -566,7 +569,11 @@ sub template_test ($$$$;$$) {
   $in =~ s/\n$// if $stripnl eq 'in' || $stripnl eq 'both';
   $out =~ s/\n$// if $stripnl eq 'out' || $stripnl eq 'both';
 
   $in =~ s/\n$// if $stripnl eq 'in' || $stripnl eq 'both';
   $out =~ s/\n$// if $stripnl eq 'out' || $stripnl eq 'both';
 
-  my $templater = Squirrel::Template->new(template_dir=>'t/templates');
+  my $templater = Squirrel::Template->new
+    (
+     template_dir=>'t/templates',
+     preload => "preload.tmpl"
+    );
 
   my $result = $templater->replace_template($in, $acts, undef, "test", $vars);
 
 
   my $result = $templater->replace_template($in, $acts, undef, "test", $vars);
 
diff --git a/t/templates/preload.tmpl b/t/templates/preload.tmpl
new file mode 100644 (file)
index 0000000..925bf92
--- /dev/null
@@ -0,0 +1,4 @@
+<:.define preloaded-:>
+This was preloaded
+<:-.end define:>
+This shouldn't show
\ No newline at end of file