SPIP

[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] [sv] [tr] [vi] [zh] Espace de traduction

Download

One template, several articles

Now it starts to become useful

October 2003

All the versions of this article:


The preceding lesson enabled us to extract the content of article no 1 from the database and build a web page from it. Let us generalize...

Our template is quite useless 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/tutorial.php3?id_article=2.

And there it appears... still article 1. Let us modify 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}.)

Now, http://yoursite.net/tutorial.php3?id_article=2 should display article 2 [1].

BOUCLE_article will be executed in a "context" where id_article is equal to 2 (i.e. the value which was passed in the URL). If you specify {id_article=1} in the template, it will seek article no 1, but if you only specify {id_article}, it will seek the article whose number is indicated by context (here the URL).

Now click on:
-  http://yoursite.net/tutorial.php3?id_article=1,
-  http://yoursite.net/tutorial.php3?id_article=2 and
-  http://yoursite.net/tutorial.php3.

Do you see the difference? The first two pages give 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

[1] it doesn’t, well it should have...


ٍShow the template of this page Site created with SPIP | Translation area | Private area