Tags:
automation1Add my vote for this tag component2Add my vote for this tag development1Add my vote for this tag create new tag
view all tags

ForEachPlugin

Loop over successive elements of a list, or a range of numbers

Syntax Rules

Tag Description
%FOREACH{"_var_" in="_list_"}% body %NEXT{"_var_"}% Loop over _body_ setting control variable _var_ to each successive element of _list_
%FOR{"_var_" start="_int_" stop="_int_" step="_int_"}% body %NEXT{"_var_"}% Loop over _body_ setting control variable _var_ to each integer in the range start to stop in steps of step

Parameters Description
FOREACH
"..." Loop control variable. Within the body of the text this may be referred to as $var. %FOREACH{"var" ...}% must have a matching %NEXT{"var"}%
in="..." Comma separated list. May contain TWiki Variables
FOR
"..." Loop control variable. Within the body of the text this may be referred to as $var. %FOR{"var" ...}% must have a matching %NEXT{"var"}%
"start"="..." Starting integer
"stop"="..." Stop integer
"step"="..." Increment/decrement var in steps of step. THIS IS REQUIRED

  • All parameters are required, and must appear in the specified order.

FOREACH Examples

Basic Usage

%FOREACH{"web" in="Main, Sandbox, TWiki"}%
   * [[$web.WebHome]]
%NEXT{"web"}%

... gives ...

... if installed. %FOREACH{"web" in="Main, Sandbox, TWiki"}%

%NEXT{"web"}%

Nested loops are possible:

%FOREACH{"i" in="1, 2, 3"}% %FOREACH{"j" in="a, b, c"}% $i$j %NEXT{"j"}% %NEXT{"i"}%
%FOREACH{"i" in="1, 2, 3"}% %FOREACH{"j" in="a, b, c"}% $i$j %NEXT{"j"}% %NEXT{"i"}%

Lists may contain TWiki Variables or other plugins.

  • The body of the loop may need to delay expansion: use $percnt to replace % if necessary.
%FOREACH{"web" in="%WEBLIST{"$name" separator=", "}%"}%
| $web | $percntFORMFIELD{"STATUS" topic="$web.%HOMETOPIC%" default="Formfield STATUS not defined in $web" alttext="Formfield STATUS not found in $web"}$percnt |
%NEXT{"web"}%
%FOREACH{"web" in="Blog, Codev, Main, Plugins, Sandbox, Support, TWiki, TWiki01, TWiki02, TWiki03, TWiki04, TWiki04x01, TWiki04x02, TWiki04x03, TWiki05x00, TWiki05x01, TWiki06x00, TWiki06x01, WikiWed"}%
$web $percntFORMFIELD{"STATUS" topic="$web.WebHome" default="Formfield STATUS not defined in $web" alttext="Formfield STATUS not found in $web"}$percnt
%NEXT{"web"}%

FOR Examples

Count up and down in steps

  • %FOR{"counta" start="1" stop="10" step="2"}% $counta %NEXT{"counta"}%
  • %FOR{"counta" start="1" stop="10" step="2"}% $counta %NEXT{"counta"}%
  • %FOR{"countb" start="1" stop="10" step="1"}% $countb %NEXT{"countb"}%
  • %FOR{"countb" start="1" stop="10" step="1"}% $countb %NEXT{"countb"}%
  • %FOR{"countc" start="10" stop="1" step="-1"}% $countc %NEXT{"countc"}%
  • %FOR{"countc" start="10" stop="1" step="-1"}% $countc %NEXT{"countc"}%

Nested loops are possible:

%FOR{"outer" start="1" stop="3" step="1"}%
%FOR{"inner" start="3" stop="1" step="-1"}%
   * $outer - $inner 
%NEXT{"inner"}%
%NEXT{"outer"}%
%FOR{"outer" start="1" stop="3" step="1"}% %FOR{"inner" start="103" stop="100" step="-1"}%
  • $outer - $inner
%NEXT{"inner"}% %NEXT{"outer"}%

Error trapping

  • %FOR{"countd" start="10" stop="1" step="1"}% $countd %NEXT{"countd"}% (invalid step)
  • %FOR{"countd" start="10" stop="1" step="1"}% $countd %NEXT{"countd"}%
  • %FOR{"counte" start="a" stop="b" step="c"}% $countd %NEXT{"counte"}% (non-integers)
  • %FOR{"counte" start="a" stop="b" step="c"}% $countd %NEXT{"counte"}%

Plugin Settings

Plugin settings are stored as preferences variables. To reference a plugin setting write %<plugin>_<setting>%, i.e. %INTERWIKIPLUGIN_SHORTDESCRIPTION%

  • One line description, is shown in the TextFormattingRules topic:
    • Set SHORTDESCRIPTION = Loop over successive elements of a list, or a range of numbers

  • Debug plugin: (See output in data/debug.txt)
    • Set DEBUG = 0

Plugin Installation Instructions

Note: You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the server where TWiki is running.

  • Download the ZIP file from the Plugin web (see below)
  • Unzip ForEachPlugin.zip in your twiki installation directory. Content:
    File: Description:
    data/TWiki/ForEachPlugin.txt Plugin topic
    lib/TWiki/Plugins/ForEachPlugin.pm Plugin Perl module
  • Test if the installation was successful:
    • See if above examples work

Plugin Info

Plugin Author: TWiki:Main.MagnusLewisSmith
Copyright: © 2005, TWiki:Main.MagnusLewisSmith;
© 2005-2010, TWiki:TWiki.TWikiContributor
License: GPL (GNU General Public License)
Plugin Version: 19384 (2010-08-28)
Change History:  
2010-08-28: TWikibug:Item6571 - doc improvements; changing TWIKIWEB to SYSTEMWEB
2005-09-28: Initial version
TWiki Dependency: $TWiki::Plugins::VERSION 1.024
CPAN Dependencies: none
Other Dependencies: none
Perl Version: 5.005
License: GPL (GNU General Public License)
TWiki:Plugins/Benchmark: GoodStyle nn%, FormattedSearch nn%, ForEachPlugin nn%
Plugin Home: http://TWiki.org/cgi-bin/view/Plugins/ForEachPlugin
Feedback: http://TWiki.org/cgi-bin/view/Plugins/ForEachPluginDev
Appraisal: http://TWiki.org/cgi-bin/view/Plugins/ForEachPluginAppraisal

Related Topics: TWikiPreferences, TWikiPlugins

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatmd5 ForEachPlugin.md5 r1 manage 0.2 K 2010-08-28 - 08:25 PeterThoeny  
Compressed Zip archivetgz ForEachPlugin.tgz r1 manage 5.1 K 2010-08-28 - 08:24 PeterThoeny  
Compressed Zip archivezip ForEachPlugin.zip r2 r1 manage 6.6 K 2010-08-28 - 08:24 PeterThoeny  
Unknown file formatext ForEachPlugin_installer r1 manage 3.4 K 2010-08-28 - 08:24 PeterThoeny  
Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r7 - 2018-07-17 - PeterThoeny
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.