TIP ForEachPlugin is not installed on Foswiki.org.


This plugin provides basic FOR and FOREACH loop constructions.

ALERT! This plugin provides some macros which do not follow normal macro expansion rules and may behave strangely under some more advanced usage scenarios (such as when CALC is involved). Please consider replacing your usage of ForEachPlugin with one of the alternatives described in Foswiki:Support.Faq39

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
"..." 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 Foswiki macros
"..." 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, System"}%
   * [[$web.WebHome]]

... gives ...

... if installed. %FOREACH{"web" in="Main, Sandbox, System"}% %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 Foswiki macros 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 |
%FOREACH{"web" in="About, Community, Community/Developers, Development, Download, Extensions, Extensions/Archived, Extensions/Testing, Home, Main, Support, Support/ClientSupport, Support/Question922, System, Tasks, Tasks/Testcases, Trash/MiguelSandbox, Trash/NC, Trash/PoojaBedur, Trash/StefanKrüger, Trash/Subweb"}%
$web $percntFORMFIELD{"STATUS" topic="$web.WebHome" default="Formfield STATUS not defined in $web" alttext="Formfield STATUS not found in $web"}$percnt

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 
%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 Foswiki is running.

  • Download the ZIP file from the Plugin web (see below)
  • Unzip ForEachPlugin.zip in your Foswiki installation directory. Content:
    File: Description:
    data/System/ForEachPlugin.txt Plugin topic
    data/System/ForEachPlugin.txt,v Plugin topic repository
    lib/Foswiki/Plugins/ForEachPlugin.pm Plugin Perl module
  • Test if the installation was successful:
    • enter samples here

Plugin Info

Change History:  
| 14 Mar 2017: | (1.103) Foswikitask:Item
22 Dec 2010: (1.102) Foswikitask:Item10183 - Add link to Foswiki:Support.Faq39
10 Nov 2010: (1.101) Foswikitask:Item9993 - Fix Release Version
04 Jun 2009: (1.100) Ported unmodified to Foswiki by Kenneth Lavrsen
28 Sep 2005: (1.000) Initial version
Foswiki Dependency: $Foswiki::Plugins::VERSION 1.024
CPAN Dependencies: none
Other Dependencies: none
Perl Version: 5.008

Related Topics: SitePreferences, Plugins

PackageForm edit

Author Original 2005: Magnus Lewis Smith - From 2009 Kenneth Lavrsen and put in Feel Free to Modify policy
Version 1.102
Release 14 Mar 2017
License GPL (GNU General Public License)
Home http://foswiki.org/Extensions/ForEachPlugin
Support http://foswiki.org/Support/ForEachPlugin
Repository https://github.com/foswiki/ForEachPlugin
ExtensionClassification Information structuring and Search
ExtensionType PluginPackage
DemoUrl http://
SupportUrl ForEachPlugin
ModificationPolicy PleaseFeelFreeToModify
I Attachment Action Size Date Who Comment
ForEachPlugin.md5md5 ForEachPlugin.md5 manage 162 bytes 30 Mar 2017 - 07:13 CrawfordCurrie  
ForEachPlugin.sha1sha1 ForEachPlugin.sha1 manage 186 bytes 30 Mar 2017 - 07:14 CrawfordCurrie  
ForEachPlugin.tgztgz ForEachPlugin.tgz manage 5 K 30 Mar 2017 - 07:13 CrawfordCurrie  
ForEachPlugin.zipzip ForEachPlugin.zip manage 7 K 30 Mar 2017 - 07:13 CrawfordCurrie  
ForEachPlugin_installerEXT ForEachPlugin_installer manage 4 K 30 Mar 2017 - 07:13 CrawfordCurrie  
Topic revision: r3 - 30 Mar 2017, CrawfordCurrie
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy