add a params "variable" that acts like the param tag
authorTony Cook <tony@develop-help.com>
Mon, 25 Feb 2013 02:38:06 +0000 (13:38 +1100)
committerTony Cook <tony@develop-help.com>
Mon, 25 Feb 2013 02:38:06 +0000 (13:38 +1100)
MANIFEST
site/cgi-bin/modules/Squirrel/Template.pm
site/cgi-bin/modules/Squirrel/Template/Params.pm [new file with mode: 0644]
t/020-templater/040-original.t

index 279fbad427fdf437040ba16e6cca38d354745b4c..1b5f609fcb6f208cefd0533e71d6452aec331f57 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -348,6 +348,7 @@ site/cgi-bin/modules/Squirrel/Template/Expr/WrapClass.pm
 site/cgi-bin/modules/Squirrel/Template/Expr/WrapCode.pm
 site/cgi-bin/modules/Squirrel/Template/Expr/WrapHash.pm
 site/cgi-bin/modules/Squirrel/Template/Expr/WrapScalar.pm
+site/cgi-bin/modules/Squirrel/Template/Params.pm
 site/cgi-bin/modules/Squirrel/Template/Parser.pm
 site/cgi-bin/modules/Squirrel/Template/Processor.pm
 site/cgi-bin/modules/Squirrel/Template/Tokenizer.pm
index 7d7d186f6d2183bbf10909194d7bbb6ae44d2556..caa976a7ed1b23e895d42662de66e98b76eff574 100644 (file)
@@ -6,6 +6,7 @@ use Squirrel::Template::Parser;
 use Squirrel::Template::Deparser;
 use Squirrel::Template::Processor;
 use Squirrel::Template::Expr;
+use Squirrel::Template::Params;
 
 use constant MAX_SCOPES => 50;
 
@@ -19,7 +20,7 @@ BEGIN {
 
 use constant DEBUG_GET_PARMS => 0;
 
-our $VERSION = "1.023";
+our $VERSION = "1.024";
 
 my %compile_cache;
 
@@ -509,7 +510,12 @@ sub replace {
 
   local $self->{scopes} = [];
   push @{$self->{scopes}}, $vars if $vars;
-  push @{$self->{scopes}}, { globals => {} };
+  my $my_scope = 
+    {
+     globals => {},
+     params => Squirrel::Template::Params->new(undef, $self, undef),
+    };
+  push @{$self->{scopes}}, $my_scope;
   local $self->{scope_contexts} = [];
 
   local $self->{defines} = {};
@@ -849,7 +855,9 @@ can then be replaced on each iteration.
 
 =back
 
-=head1 The loop variable
+=head1 Special Variables
+
+=head2 The loop variable
 
 Each C<.for> loop defines a C<loop> variable.  If you have nested
 loops, you can define an alias to the variable, eg:
@@ -905,6 +913,21 @@ list - the list argument to C<.for>.
 
 =back
 
+=head2 params
+
+This is set to the names and values supplied in a C<wrap> request, so:
+
+  <:= param.name :>
+
+is equivalent to:
+
+  <: param name :>
+
+=head2 globals
+
+C<globals> provides a top-level hash to fill as required.  This can be
+used to pass information from an inner-scope back to an outer scope.
+
 =head1 OLD TEMPLATE SYNTAX
 
 This is the older template syntax that is retained for compatibility.
diff --git a/site/cgi-bin/modules/Squirrel/Template/Params.pm b/site/cgi-bin/modules/Squirrel/Template/Params.pm
new file mode 100644 (file)
index 0000000..20f6413
--- /dev/null
@@ -0,0 +1,23 @@
+package Squirrel::Template::Params;
+use strict;
+use base "Squirrel::Template::Expr::WrapBase";
+use Scalar::Util ();
+
+our $VERSION = "1.000";
+
+sub new {
+  my ($class, @rest) = @_;
+
+  my $self = $class->SUPER::new(@rest);
+  Scalar::Util::weaken($self->[1]);
+
+  return $self;
+}
+
+sub call {
+  my ($self, $method, $args) = @_;
+
+  return $self->[1]->tag_param($method);
+}
+
+1;
index 705babb1874af10c3873408d009c4c1c3912cf36..b703e6e9e3ce6090c23046c6533a1facd53937a4 100644 (file)
@@ -97,11 +97,13 @@ TEMPLATE
 <:wrap wraptest.tmpl title=>[cat "foo " [str]], menu => 1, showtitle => "abc" :>Alpha
 <:param menu:>
 <:param showtitle:>
+<:= params.showtitle :>
 TEMPLATE
 <title>foo ABC</title>
 Alpha
 1
 abc
+abc
 OUTPUT
 
   template_test(<<TEMPLATE, <<OUTPUT, "wrap", \%acts, "both");