From 27a094e18c6af6315f507be638170175248ce50f Mon Sep 17 00:00:00 2001 From: Angel Ortega Date: Thu, 20 Dec 2007 13:34:25 +0100 Subject: [PATCH] New Artemus function foreach() (that should go into Artemus itself). --- Gruta/Template/Artemus.pm | 26 ++++++++++++++++++++++++++ examples/g.pl | 2 ++ 2 files changed, 28 insertions(+) diff --git a/Gruta/Template/Artemus.pm b/Gruta/Template/Artemus.pm index 0ba6876..9f1a259 100644 --- a/Gruta/Template/Artemus.pm +++ b/Gruta/Template/Artemus.pm @@ -46,6 +46,32 @@ sub _artemus { $f{date} = sub { Gruta::Data::today(); }; $f{random} = sub { $_[rand(scalar(@_))]; }; + $f{foreach} = sub { + my $list = shift; + my $code = shift; + my $sep = shift; + my @ret = (); + + $code =~ s/\[/{/g; + $code =~ s/\]/}/g; + + foreach my $e (split(/;/, $list)) { + my @e = split(/,/, $e); + + my $c = $code; + my $n = 0; + + foreach my $i (@e) { + $c =~ s/%$n/$i/g; + $n++; + } + + push(@ret, $c); + } + + return join($sep, @ret); + }; + foreach my $p (Gruta::Data::Topic->new->afields()) { $f{'topic_' . $p} = sub { my $topic = shift; diff --git a/examples/g.pl b/examples/g.pl index c7a294a..2fdb26f 100644 --- a/examples/g.pl +++ b/examples/g.pl @@ -53,6 +53,8 @@ my $w = Gruta->new( cgi => Gruta::CGI->new() ); +my $ret = $tmpl->process('{-foreach|a1,a2;b1,b2;c1,c2|cell %0: "%1 * 3"|,}'); + $w->login('angel', 'test'); $w->logout(); -- 2.11.4.GIT