• New in : SPIP 2.0
  • Deleted since : SPIP 3.0

The #FOREACH tag is used to "loop" through a content equivalent to a table array calculated within the template file, by applying a SPIP model to each of its member elements.

#FOREACH{tag, model}


-  Within any template, using #FOREACH{env} will display all the variables of the #ENV tag as shown below:

  • page=> test_loop
  • date=> 2009-12-22 22:44:27
  • date_default=> 1
  • date_redac=> 2009-12-22 22:44:27
  • date_redac_default=> 1

In other words, for each element in the associative PHP table that is presented by the #ENV tag, #FOREACH will apply the model file called foreach.html (cf. further below).

-  We can likewise display the contents of another tag, e.g. #CONFIG. For this tag, we pass the name of the tag as the first parameter:

#FOREACH{config} will then display something like:

  • charset_sql_base=> utf8
  • charset_collation_sql_base=> utf8_general_ci
  • charset_sql_connexion=> utf8
  • version_installee=> 14558
  • nouvelle_install=> 1
  • plugin=> a:3:{s:3:"CFG";a:4:{s:3:"nom";s:90:"<multi> [fr]cfg: moteur de configuration [de]cfg: Konfigurationsmotor </multi>";s:4:"etat";s:6:"stable";s:3:"dir";s:8:"auto/cfg";s:7:"version";s:6:"1.14.1";}}
  • derniere_modif_rubrique=> 1260746188
  • ...=> ...

And for any SPIP tag that returns a PHP table, we will see a somewhat similar kind of result (see #ARRAY).

Creating a dedicated model

The foreach.html model is the one applied by default to such "loops". It is found in the squelettes-dist/modeles/ directory. It is possible to modify it directly, but you can also crete a special model for a tag and store it in its own squelettes/modeles/ folder.

The foreach_tag.html model corresponds to any #FOREACH{tag} usage.

We can also maybe hope to create generalised models and use them for a variety of tags.
For example, for unnumbered lists we could have a model called squelettes/modeles/foreach_ul_li.html which would contain: <li><a href="#ENV{valeur}">#ENV{cle}</a></li> and would be called by [<ul>(#FOREACH{ENV, foreach_ul_li})</ul>], thereby generating the following HTML source code:

<li><a href="test_loop">page</a></li>
<li><a href="2009-12-22 22:54:40">date</a></li>
<li><a href="1">date_default</a></li>
<li><a href="2009-12-22 22:54:40">date_redac</a></li>
<li><a href="1">date_redac_default</a></li>

More values

Finally, for more complex cases, we can pass more values to the model by respecting the structure of the following table for the tag that you have designed:

array (
    'key' => array (
        'val1' => 'value1'
        'val2' => 'value2'
        'val3' => 'value3'
        'val4' => 'value4'
    'key2' => array (
        'val1' => 'value1sub'
        'val2' => 'value2sub'
        'val3' => 'value3sub'
        'val4' => 'value4sub'

and by supplying a suitably modified model:

#ENV{val1}, #ENV{val2}, #ENV{val3} and #ENV{val4}

Author Mark Published : Updated : 02/06/21

Translations : català, English, Español, français, Nederlands