[ar] [ast] [bg] [br] [ca] [co] [cpf] [cs] [da] [de] [en] [eo] [es] [eu] [fa] [fon] [fr] [gl] [id] [it] [ja] [lb] [nl] [oc] [pl] [pt] [ro] [ru] [sk] [sv] [tr] [uk] [vi] [zh] Espace de traduction


A section

or: How to make lists using database content

October 2003 — updated on : February 2012

All the versions of this article:

In the preceding lesson we learned how to display elements according to the context. Here we will see how this context varies depending on the loop, BOUCLES, being processed.

Let us modify our "tutorial.html" template in the following way:

Here, we’ve removed the {id_article} condition altogether. A word of caution, this loop can generate a very large page if your database contains a lot of articles; it is worth taking precautions by adding {0,10} to limit the loop to the first 10 articles.


And the result, simply calling http://yoursite.net/spip.php?page=tutorial [*] (no need for id_article from now on, since this condition was removed from the template), the titles of the first 10 published articles are displayed, each on a separate line. From this example, you can see how you can produce the synopsis of a section. Now let us display the 10 most recent articles in this section:

Let’s look through this code bit by bit:
-  id_rubrique: only includes the articles belonging to the id_rubrique section (see below that this variable is defined in the context of our BOUCLE_article);
-  {par date}{inverse}: sort by date starting with the most recent.
-  {0,10}: ... and take only the first 10 results.
-  Lastly, the code <a href="#URL_ARTICLE">#TITRE</a> will display not only the title of the article but also generate links to those articles.

All that remains is to call the template, while passing the context id_rubrique=1:

http://yoursite.net/spip.php?page=tutorial&id_rubrique=1 [*]

The magic of SPIP lies in combining these types of functionality for more complex or specific results. If you already understand this point, you’re well on the road to great success!


[*Remember that in versions prior to SPIP 1.9, the URL to display out tutorial page would have been: http://yoursite.net/tutorial.php3. And if we want to pass it a parameter we would have coded: http://yoursite.net/tutorial.php3?id_rubrique=1, etc.

Show the template of this page Site powered by SPIP | Translation area | Private area