One template, several articles

In the previous lesson we extracted data from article 1 of the database and turned it into a web page. Let’s generalise...

Our template is not terribly useful if it can only display article no 1. Let’s enhance it to display any article:

For that we will call our web page with a parameter, id_article=2. Type the following into your web browser http://yoursite.net/spip.php?page=tutorial&id_article=2.

And there it appears... still article 1. So let’s fix that problem by modifying the line in the tutorial.html template that defines the "article loop":
<BOUCLE_article(ARTICLES){id_article}>

(As you can see, you simply replace {id_article=1} with {id_article}.)

And now, http://yoursite.net/spip.php?page=tutorial&id_article=2 should display article 2 [1].

The loop called BOUCLE_article will be executed inside a "context" that includes the id_article variable having a value of 2 (i.e. the value which was passed in the URL). If you specify {id_article=1} in the template, it will specifically find article no 1, but if you only specify {id_article}, it will find the article whose number is indicated by the context (in this case, by the URL).

Now click on:
-  http://yoursite.net/spip.php?page=tutorial&id_article=1,
-  http://yoursite.net/spip.php?page=tutorial&id_article=2 and
-  http://yoursite.net/spip.php?page=tutorial.

Do you see the difference? The first two pages show you articles no 1 and 2, the third does not have a id_article in its context, and generates an error.

Congratulations! Your template is now "contextual".

Footnotes

[1Well it should...

Author Paul Knight Published : Updated : 08/06/23

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