The syntax of loops

A loop typically declares both a database table from which to extract information as well as the criteria to be used for selecting a subset of the table’s records.

<BOUCLE_loopname(TABLE){criterion1}{criterion2}>
 ... code to be applied for each member object selected by the loop ...
</BOUCLE_loopname>

Every loop has it’s own name identifier which must be unique within the template file. This name is used together with the reserved word “BOUCLE” (English: "loop") in order to mark the start and the end of the loop. In this example, the loop’s name is “_loopname”.

The table is specified either by an alias (written in capital letters) or by the real name of the table (matching the case), for example “spip_articles”. The example uses the “TABLE” alias.

This loop declaration is then followed by the criteria, each of which is always written enclosed in braces. For example, the criterion {par nom} will sort the results according in ascending order of the contents of the “nom” column of the database table.

Example

This loop lists all of the images stored in the database. It draws its data from the database using the DOCUMENTS alias, and the selection criterion {extension IN jpg,png,gif} will retrieve only those files which have a filename extension that matches one of the three extensions in the given list.

<BOUCLE_documents(DOCUMENTS){extension IN jpg,png,gif}>
    [(#FICHIER|image_reduire{300})]
</BOUCLE_documents>

The #FICHIER tag contains the URL of the image document. Using the pipe symbol, a filter named “image_reduire” is then applied to the tag. This particular filter performs as a function that resizes the original image to be at most 300 pixels (in height AND width) and returns an HTML <img> tag for the image that has been generated and scaled as specified.

Author Thomas Sutton Published : Updated : 07/09/17

Translations : English, Español, français