ForEachPlugin

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

... gives ...

... if installed.

Nested loops are possible:

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

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 |
%NEXT{"web"}%
Apps Formfield STATUS not found in Apps
Help Formfield STATUS not found in Help
Learn Formfield STATUS not found in Learn
Main Formfield STATUS not found in Main
Main/Admin Formfield STATUS not found in Main/Admin
Main/Plugins Formfield STATUS not found in Main/Plugins
Main_dist Formfield STATUS not found in Main_dist
Philtres Formfield STATUS not found in Philtres
Projects Formfield STATUS not found in Projects
Projects/AT_Cat Formfield STATUS not found in Projects/AT_Cat
Projects/AT_Cat/Catalog Formfield STATUS not found in Projects/AT_Cat/Catalog
Projects/AT_Cat/Offering Formfield STATUS not found in Projects/AT_Cat/Offering
Projects/AT_Cat/Offering/Computer Formfield STATUS not found in Projects/AT_Cat/Offering/Computer
Projects/AT_Cat/Offering/Computer/Hardware Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Hardware
Projects/AT_Cat/Offering/Computer/Service Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Service
Projects/AT_Cat/Offering/Computer/Service/Content Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Service/Content
Projects/AT_Cat/Offering/Computer/Service/Course Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Service/Course
Projects/AT_Cat/Offering/Computer/Service/Support Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Service/Support
Projects/AT_Cat/Offering/Computer/Software Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/Software
Projects/AT_Cat/Offering/Computer/System Formfield STATUS not found in Projects/AT_Cat/Offering/Computer/System
Projects/AT_Cat/Offering/Domestic Formfield STATUS not found in Projects/AT_Cat/Offering/Domestic
Projects/AT_Cat/Offering/Domestic/Kitchen Formfield STATUS not found in Projects/AT_Cat/Offering/Domestic/Kitchen
Projects/AT_Cat/Offering/Event Formfield STATUS not found in Projects/AT_Cat/Offering/Event
Projects/AT_Cat/Offering/Event/Conference Formfield STATUS not found in Projects/AT_Cat/Offering/Event/Conference
Projects/AT_Cat/Organization Formfield STATUS not found in Projects/AT_Cat/Organization
Projects/AT_Cat/Organization/Academic Formfield STATUS not found in Projects/AT_Cat/Organization/Academic
Projects/AT_Cat/Organization/Corporate Formfield STATUS not found in Projects/AT_Cat/Organization/Corporate
Projects/AT_Cat/Organization/Government Formfield STATUS not found in Projects/AT_Cat/Organization/Government
Projects/AT_Cat/Organization/Non_Profit Formfield STATUS not found in Projects/AT_Cat/Organization/Non_Profit
Projects/AT_Cat/Organization/Standardization Formfield STATUS not found in Projects/AT_Cat/Organization/Standardization
Projects/Access Formfield STATUS not found in Projects/Access
Projects/Access/Online Formfield STATUS not found in Projects/Access/Online
Projects/Access/Utiles Formfield STATUS not found in Projects/Access/Utiles
Projects/Access/Utiles/AR Formfield STATUS not found in Projects/Access/Utiles/AR
Projects/Access/Utiles/AR/US Formfield STATUS not found in Projects/Access/Utiles/AR/US
Projects/Access/Utiles/AS Formfield STATUS not found in Projects/Access/Utiles/AS
Projects/Access/Utiles/BE Formfield STATUS not found in Projects/Access/Utiles/BE
Projects/Access/Utiles/CR Formfield STATUS not found in Projects/Access/Utiles/CR
Projects/Access/Utiles/DS Formfield STATUS not found in Projects/Access/Utiles/DS
Projects/Access/Utiles/Fab Formfield STATUS not found in Projects/Access/Utiles/Fab
Projects/Access/Utiles/Fab/BE Formfield STATUS not found in Projects/Access/Utiles/Fab/BE
Projects/Access/Utiles/Fab/LE Formfield STATUS not found in Projects/Access/Utiles/Fab/LE
Projects/Access/Utiles/Fab/SP Formfield STATUS not found in Projects/Access/Utiles/Fab/SP
Projects/Access/Utiles/Fab/VF Formfield STATUS not found in Projects/Access/Utiles/Fab/VF
Projects/Access/Utiles/HW Formfield STATUS not found in Projects/Access/Utiles/HW
Projects/Access/Utiles/MD Formfield STATUS not found in Projects/Access/Utiles/MD
Projects/Access/Utiles/QR Formfield STATUS not found in Projects/Access/Utiles/QR
Projects/Access/Utiles/QR/UTA Formfield STATUS not found in Projects/Access/Utiles/QR/UTA
Projects/Access/Utiles/QR/UTA/GDC Formfield STATUS not found in Projects/Access/Utiles/QR/UTA/GDC
Projects/Access/Utiles/RX Formfield STATUS not found in Projects/Access/Utiles/RX
Projects/Access/Utiles/SM Formfield STATUS not found in Projects/Access/Utiles/SM
Projects/Access/Utiles/Tango Formfield STATUS not found in Projects/Access/Utiles/Tango
Projects/Access/Utiles/UE_save Formfield STATUS not found in Projects/Access/Utiles/UE_save
Projects/Access/Utiles/WA Formfield STATUS not found in Projects/Access/Utiles/WA
Projects/Arborescence Formfield STATUS not found in Projects/Arborescence
Projects/Arti Formfield STATUS not found in Projects/Arti
Projects/Arti/Design Formfield STATUS not found in Projects/Arti/Design
Projects/Arti/Patterns Formfield STATUS not found in Projects/Arti/Patterns
Projects/AutoCoda Formfield STATUS not found in Projects/AutoCoda
Projects/AxAp Formfield STATUS not found in Projects/AxAp
Projects/AxAp/EPUB Formfield STATUS not found in Projects/AxAp/EPUB
Projects/AxAp/EPUB/Ex Formfield STATUS not found in Projects/AxAp/EPUB/Ex
Projects/AxAp/EPUB/KI Formfield STATUS not found in Projects/AxAp/EPUB/KI
Projects/AxAp/News Formfield STATUS not found in Projects/AxAp/News
Projects/AxAp/Text Formfield STATUS not found in Projects/AxAp/Text
Projects/AxArd Formfield STATUS not found in Projects/AxArd
Projects/AxArd/Grove Formfield STATUS not found in Projects/AxArd/Grove
Projects/Clojure Formfield STATUS not found in Projects/Clojure
Projects/Clojure/Key Formfield STATUS not found in Projects/Clojure/Key
Projects/Codeq Formfield STATUS not found in Projects/Codeq
Projects/Coding_Idioms Formfield STATUS not found in Projects/Coding_Idioms
Projects/Coding_Idioms/Clojure Formfield STATUS not found in Projects/Coding_Idioms/Clojure
Projects/Coding_Idioms/SUMO Formfield STATUS not found in Projects/Coding_Idioms/SUMO
Projects/Connascence Formfield STATUS not found in Projects/Connascence
Projects/D3 Formfield STATUS not found in Projects/D3
Projects/DT_Logger Formfield STATUS not found in Projects/DT_Logger
Projects/Datomic Formfield STATUS not found in Projects/Datomic
Projects/Elixir Formfield STATUS not found in Projects/Elixir
Projects/Elixir/CE Formfield STATUS not found in Projects/Elixir/CE
Projects/Elixir/DA Formfield STATUS not found in Projects/Elixir/DA
Projects/Elixir/DS Formfield STATUS not found in Projects/Elixir/DS
Projects/Elixir/EN Formfield STATUS not found in Projects/Elixir/EN
Projects/Elixir/ES Formfield STATUS not found in Projects/Elixir/ES
Projects/Elixir/Erlex Formfield STATUS not found in Projects/Elixir/Erlex
Projects/Elixir/HTD Formfield STATUS not found in Projects/Elixir/HTD
Projects/Elixir/Key Formfield STATUS not found in Projects/Elixir/Key
Projects/Elixir/Nerves Formfield STATUS not found in Projects/Elixir/Nerves
Projects/Elixir/Nerves/H2M Formfield STATUS not found in Projects/Elixir/Nerves/H2M
Projects/Elixir/OD Formfield STATUS not found in Projects/Elixir/OD
Projects/Elixir/PD Formfield STATUS not found in Projects/Elixir/PD
Projects/Elixir/PF Formfield STATUS not found in Projects/Elixir/PF
Projects/Elixir/PG Formfield STATUS not found in Projects/Elixir/PG
Projects/Elixir/Spex Formfield STATUS not found in Projects/Elixir/Spex
Projects/Elixir/TE Formfield STATUS not found in Projects/Elixir/TE
Projects/GM Formfield STATUS not found in Projects/GM
Projects/GM/GM_j Formfield STATUS not found in Projects/GM/GM_j
Projects/GM/GM_k Formfield STATUS not found in Projects/GM/GM_k
Projects/GM/GM_k2 Formfield STATUS not found in Projects/GM/GM_k2
Projects/GitLab Formfield STATUS not found in Projects/GitLab
Projects/GraphMagic Formfield STATUS not found in Projects/GraphMagic
Projects/GraphMagic/CFCL_h Formfield STATUS not found in Projects/GraphMagic/CFCL_h
Projects/GraphMagic/CFCL_i Formfield STATUS not found in Projects/GraphMagic/CFCL_i
Projects/GraphMagic/CFCL_j Formfield STATUS not found in Projects/GraphMagic/CFCL_j
Projects/Guitar Formfield STATUS not found in Projects/Guitar
Projects/Guitar/News Formfield STATUS not found in Projects/Guitar/News
Projects/HAND Formfield STATUS not found in Projects/HAND
Projects/ISLE Formfield STATUS not found in Projects/ISLE
Projects/ISLE/Concepts Formfield STATUS not found in Projects/ISLE/Concepts
Projects/ISLE/H_AE Formfield STATUS not found in Projects/ISLE/H_AE
Projects/ISLE/IPM_Lab Formfield STATUS not found in Projects/ISLE/IPM_Lab
Projects/ISLE/MSEAS_ATL Formfield STATUS not found in Projects/ISLE/MSEAS_ATL
Projects/ISLE/SCIPM_Manual Formfield STATUS not found in Projects/ISLE/SCIPM_Manual
Projects/ISLE/S_AE Formfield STATUS not found in Projects/ISLE/S_AE
Projects/ISLE/S_AE_ehh Formfield STATUS not found in Projects/ISLE/S_AE_ehh
Projects/ISLE/S_PP Formfield STATUS not found in Projects/ISLE/S_PP
Projects/MBW Formfield STATUS not found in Projects/MBW
Projects/MBW/MBW_d Formfield STATUS not found in Projects/MBW/MBW_d
Projects/MBW/MBW_e Formfield STATUS not found in Projects/MBW/MBW_e
Projects/MBW/MBW_f Formfield STATUS not found in Projects/MBW/MBW_f
Projects/Merbiana Formfield STATUS not found in Projects/Merbiana
Projects/Meta Formfield STATUS not found in Projects/Meta
Projects/Morinfo Formfield STATUS not found in Projects/Morinfo
Projects/Morinfo/Test Formfield STATUS not found in Projects/Morinfo/Test
Projects/Morinfo/Unix_OSX Formfield STATUS not found in Projects/Morinfo/Unix_OSX
Projects/Morinfo/Unix_OSX/Fact Formfield STATUS not found in Projects/Morinfo/Unix_OSX/Fact
Projects/Morinfo/Unix_OSX/Note Formfield STATUS not found in Projects/Morinfo/Unix_OSX/Note
Projects/Morinfo/Unix_OSX/Rule Formfield STATUS not found in Projects/Morinfo/Unix_OSX/Rule
Projects/Muddy Formfield STATUS not found in Projects/Muddy
Projects/NB Formfield STATUS not found in Projects/NB
Projects/Neo4j Formfield STATUS not found in Projects/Neo4j
Projects/NoBat Formfield STATUS not found in Projects/NoBat
Projects/NuPIC Formfield STATUS not found in Projects/NuPIC
Projects/Ontiki Formfield STATUS not found in Projects/Ontiki
Projects/Ontiki/OV Formfield STATUS not found in Projects/Ontiki/OV
Projects/Ontiki_2009 Formfield STATUS not found in Projects/Ontiki_2009
Projects/PDFinder Formfield STATUS not found in Projects/PDFinder
Projects/Perky Formfield STATUS not found in Projects/Perky
Projects/ReMedia Formfield STATUS not found in Projects/ReMedia
Projects/Ruby Formfield STATUS not found in Projects/Ruby
Projects/RubyKif Formfield STATUS not found in Projects/RubyKif
Projects/SPARQLy Formfield STATUS not found in Projects/SPARQLy
Projects/SPARQLy/GUIs Formfield STATUS not found in Projects/SPARQLy/GUIs
Projects/SPARQLy/Ruby Formfield STATUS not found in Projects/SPARQLy/Ruby
Projects/STEMS Formfield STATUS not found in Projects/STEMS
Projects/SemWeb Formfield STATUS not found in Projects/SemWeb
Projects/SketchUp Formfield STATUS not found in Projects/SketchUp
Projects/SketchUp/Cookbook Formfield STATUS not found in Projects/SketchUp/Cookbook
Projects/SketchUp/Drafts Formfield STATUS not found in Projects/SketchUp/Drafts
Projects/SketchUp/SketchApps Formfield STATUS not found in Projects/SketchUp/SketchApps
Projects/Spect Formfield STATUS not found in Projects/Spect
Projects/Template Formfield STATUS not found in Projects/Template
Projects/Template_0 Formfield STATUS not found in Projects/Template_0
Projects/YAGO Formfield STATUS not found in Projects/YAGO
Rich Formfield STATUS not found in Rich
Rich/Twitter Formfield STATUS not found in Rich/Twitter
Sandbox Formfield STATUS not found in Sandbox
Sandbox_dist Formfield STATUS not found in Sandbox_dist
System Formfield STATUS not found in System
Typedia Formfield STATUS not found in Typedia
Typedia/Share Formfield STATUS not found in Typedia/Share
Typedia/Tarot Formfield STATUS not found in Typedia/Tarot
Users Formfield STATUS not found in Users
Users/RichMorin Formfield STATUS not found in Users/RichMorin
Users/VickiBrown Formfield STATUS not found in Users/VickiBrown
Work Formfield STATUS not found in Work

FOR Examples

Count up and down in steps

  • %FOR{"counta" start="1" stop="10" step="2"}% $counta %NEXT{"counta"}%
  • 1 3 5 7 9
  • %FOR{"countb" start="1" stop="10" step="1"}% $countb %NEXT{"countb"}%
  • 1 2 3 4 5 6 7 8 9 10
  • %FOR{"countc" start="10" stop="1" step="-1"}% $countc %NEXT{"countc"}%
  • 10 9 8 7 6 5 4 3 2 1

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"}%
  • 1 - 103
  • 1 - 102
  • 1 - 101
  • 1 - 100
  • 2 - 103
  • 2 - 102
  • 2 - 101
  • 2 - 100
  • 3 - 103
  • 3 - 102
  • 3 - 101
  • 3 - 100

Error trapping

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

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
Description
Repository https://github.com/foswiki/ForEachPlugin
Copyright
License GPL (GNU General Public License)
Home http://foswiki.org/Extensions/ForEachPlugin
Support http://foswiki.org/Support/ForEachPlugin
Topic revision: r3 - 06 Jul 2017, VickiBrown

This site is powered by FoswikiCopyright © by the contributing authors. All material on this site is the property of the contributing authors.
Ideas, requests, problems regarding CFCL Wiki? Send feedback