Loops within loops

Displaying content from several levels

We’re going to display elements sourced from different locations on the same page.

We know how to generate a list of titles in a section. Now we are going to display the section’s own elements on the same page: its title, its explanatory text, etc.

Give it a try.

Here’s one possible solution:

<BOUCLE_rubrique(RUBRIQUES){id_rubrique}>
	<h1>#TITRE</h1>
	<BOUCLE_article(ARTICLES){id_rubrique}{par date}{inverse}{0,10}>
		<a href="#URL_ARTICLE">#TITRE</a><br>
	</BOUCLE_article>
	[(#TEXTE|justifier)]
</BOUCLE_rubrique>

We call the page with the URL http://yoursite.net/spip.php?page=tutorial&id_rubrique=1
So, what’s happening here?

Our ARTICLES loop is integrated inside a RUBRIQUES (section) loop. The ARTICLES loop’s context is the id_rubrique generated by the RUBRIQUES loop, which will itself have the context given by the URL (id_rubrique=1). Therefore we are at the ARTICLES level, within the requested id_rubrique. At this point nothing has changed.

However, the RUBRIQUES loop allows SPIP to select the field values for the section in question: we can thus display the section’s #TITRE and #TEXTE. Note that this instance of #TEXTE would be that of the section even if we were also to call the article’s #TEXTE within the ARTICLES loop. The display function of SPIP guarantees that #TEXTE of an article does not interfere with identically named tags in the outer loop.

Finally, a filter has been introduced |justifier on the #TEXTE field. This filter justifies the contents of the text before inserting it in the final page.

Author Paul Knight Published : Updated : 08/10/22

Translations : عربي, català, corsu, Deutsch, English, Español, français, italiano, Nederlands, polski, Português, русский, slovenčina, Türkçe, українська