[interchange-cvs] interchange - heins modified lib/Vend/Interpolate.pm
interchange-core@icdevgroup.org
interchange-core@icdevgroup.org
Thu Aug 1 23:23:00 2002
User: heins
Date: 2002-08-02 03:22:27 GMT
Modified: lib/Vend Interpolate.pm
Log:
* Add super-fast iterator mode for lists.
Basic premise is like this:
[tree
iterator=3Dtree_link
start=3D"[control tree_selector Products]"
table=3Dtree
master=3Dparent_fld
subordinate=3Dcode
autodetect=3D1
toggle=3Dtoggle
memo=3Dmemo
sort=3Dcode
collapse=3Dcollapse
explode=3Dexplode
spacer=3D" "
spacing=3D4
/]
This iterates the tree using sub "iterator", which can either
be a catalog Sub or a GlobalSub. In this case, no template text
is provided. If you want to provide a template, you can use
the [tree] TEMPLATE [/tree], and the template text is passed
as the first argument.
With access to the tag options, it allows you to build list-like
operations without the trouble of building your own loop-handling
and querying.
A demonstration of this will be in the product_tree component for
the "foundation" demo, which produces either a Dynamic HTML
list (no page access necessary) or falls back to a regular tree
list which requires a reshoot every time.
Works very well when combined with $Tag->attr_list, as in this
simpler tree routine:
Sub tree_link <<EOS
sub {
my $template =3D shift || qq{
{mv_spacer}{mv_children?}<A href=3D"{toggle_url}"
class=3Dbarlink
>{toggle_anchor}</A>{/mv_children?}{url?}<A href=3D"{url}"
class=3Dbarlink>{/url?}{name?}{name}{/name?}{url?}</a>{/url?}<br>
};
my $row =3D shift;
if($row->{page}) {
unless($row->{form} =3D~ /\r/) {
$row->{form} =3D join "\n", split /\&/, $row->{form};
}
$row->{url} =3D $Tag->area( { href =3D> $row->{page}, form =3D> $row->{fo=
rm} });
}
$row->{name} =3D~ s/ / /g;
if($row->{mv_children}) {
my $u =3D $Tag->history_scan( { var_exclude =3D> 'toggle,collapse,expand'=
});
$u .=3D $u =3D~ /\?/ ? '&' : "?";
$u .=3D "toggle=3D$row->{code}";
$row->{toggle_url} =3D $u;
$row->{toggle_anchor} =3D $Scratch->{memo}{$row->{code}} ? '-' : '+';
}
return $Tag->attr_list($row, $template);
}
EOS
Revision Changes Path
2.97 +45 -12 interchange/lib/Vend/Interpolate.pm
rev 2.97, prev_rev 2.96
Index: Interpolate.pm
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /anon_cvs/repository/interchange/lib/Vend/Interpolate.pm,v
retrieving revision 2.96
retrieving revision 2.97
diff -u -r2.96 -r2.97
--- Interpolate.pm 2 Aug 2002 01:33:34 -0000 2.96
+++ Interpolate.pm 2 Aug 2002 03:22:27 -0000 2.97
@@ -1,6 +1,6 @@
# Vend::Interpolate - Interpret Interchange tags
#=20
-# $Id: Interpolate.pm,v 2.96 2002/08/02 01:33:34 mheins Exp $
+# $Id: Interpolate.pm,v 2.97 2002/08/02 03:22:27 mheins Exp $
#
# Copyright (C) 1996-2002 Red Hat, Inc. <interchange@redhat.com>
#
@@ -27,7 +27,7 @@
require Exporter;
@ISA =3D qw(Exporter);
=20
-$VERSION =3D substr(q$Revision: 2.96 $, 10);
+$VERSION =3D substr(q$Revision: 2.97 $, 10);
=20
@EXPORT =3D qw (
=20
@@ -3940,7 +3940,7 @@
return '' if ! $obj;
=20
my $ary =3D $obj->{mv_results};
- return if (! $ary or ! ref $ary or ! $text or ! defined $ary->[0]);
+ return if (! $ary or ! ref $ary or ! defined $ary->[0]);
=20=09
my $save_unsafe =3D $MVSAFE::Unsafe || '';
$MVSAFE::Unsafe =3D 1;
@@ -4311,8 +4311,10 @@
my ($i, $end, $count, $text, $ary, $opt_select, $fh, $opt) =3D @_;
=20
my $r =3D '';
+ $opt ||=3D {};
+
# Optimize for no-match, on-match, etc
- if($text !~ /\[(?:if-)?$Prefix-/) {
+ if(! $opt->{iterator} and $text !~ /\[(?:if-)?$Prefix-/) {
for(; $i <=3D $end; $i++) {
$r .=3D $text;
}
@@ -4349,6 +4351,24 @@
$Vend::Cfg->{Sub}{$name} =3D $sub;
}
=20
+ my $oexec =3D { %$opt };
+
+ if($opt->{iterator}) {
+ my $sub =3D $Vend::Cfg->{Sub}{$opt->{iterator}}
+ || $Global::GlobalSub->{$opt->{iterator}};
+ if(! $sub) {
+ logError(
+ "list iterator subroutine '%s' called but not defined. Skipping.",
+ $opt->{iterator},
+ );
+ return '';
+ }
+ for( ; $i <=3D $end ; $i++ ) {
+ $r .=3D $sub->($text, $ary->[$i], $oexec);
+ }
+ return $r;
+ }
+
1 while $text =3D~ s{(\[(if[-_]$Prefix[-_][a-zA-Z]+)(?=3D.*\[\2)\s.*\[/\2=
\])}
{
resolve_nested_if($1, $2)
@@ -4363,9 +4383,6 @@
$run =3D~ s#$IB$QR{_param_if}# defined $fh->{$3} ||
::logOnce(@field_msg, $3, "if-$Orig_prefix-param") #ige;
=20
- $opt ||=3D {};
- my $oexec =3D { %$opt };
-
for( ; $i <=3D $end ; $i++, $count++ ) {
$row =3D $ary->[$i];
last unless defined $row;
@@ -4450,18 +4467,16 @@
my($i, $end, $count, $text, $hash, $opt_select, $opt) =3D @_;
=20
my $r =3D '';
+ $opt ||=3D {};
=20
# Optimize for no-match, on-match, etc
- # Ugly second regex is for quantity-name/modifier-name, wish they would
- # go away
- if($text !~ /\[/) {
+ if(! $opt->{iterator} and $text !~ /\[/) {
for(; $i <=3D $end; $i++) {
$r .=3D $text;
}
return $r;
}
=20
- $opt ||=3D {};
my $code_field =3D $opt->{code_field} || 'mv_sku';
my ($run, $code, $return, $item);
=20
@@ -4485,12 +4500,30 @@
}
#::logDebug("subhidden: $opt->{subhidden}");
=20
+ my $oexec =3D { %$opt };
+
+ if($opt->{iterator}) {
+ my $sub =3D $Vend::Cfg->{Sub}{$opt->{iterator}}
+ || $Global::GlobalSub->{$opt->{iterator}};
+ if(! $sub) {
+ logError(
+ "list iterator subroutine '%s' called but not defined. Skipping.",
+ $opt->{iterator},
+ );
+ return '';
+ }
+
+ for( ; $i <=3D $end ; $i++ ) {
+ $r .=3D $sub->($text, $hash->[$i], $oexec);
+ }
+ return $r;
+ }
+
1 while $text =3D~ s{(\[(if[-_]$Prefix[-_][a-zA-Z]+)(?=3D.*\[\2)\s.*\[/\2=
\])}
{
resolve_nested_if($1, $2)
}se;
=20
- my $oexec =3D { %$opt };
=20
for ( ; $i <=3D $end; $i++, $count++) {
$item =3D $hash->[$i];