The idea
Let’s say you often use the same list of values in the IN criterium of a loop.
example in the following loop :
<BOUCLE_art(ARTICLES){id_article IN 12,13,14,25}>
<h1>#TITRE</h1>
</BOUCLE_art>
The preceding loop lists items IDs 12, 13, 14 or 25.
A solution for sharing your list in a single skeleton
One option for sharing this list is to load it into a static array (see #ARRAYS on the use of arrays). We can then use it several times in the same skeleton (which makes it easier to maintain), for example :
[(#SET{myarray,#ARRAY{0,12,1,13,2,14,3,25}})]
<BOUCLE_art1(ARTICLES){id_article IN #GET{myarray}|sinon{0}}{par hasard}>
<h1>#TITRE</h1>
</BOUCLE_art1>
<BOUCLE_art2(ARTICLES){id_article IN #GET{myarray}|sinon{0}}{par titre}>
<h1>#TITRE</h1>
</BOUCLE_art2>
A solution for all the skeletons
Let’s imagine you use that list in several skeletons.
Here’s one way to share the list :
You create a simple skeleton which only contains a string formed from the list of values separated by a rarely-used character (eg the pipe ’|’) :
For example I create the file mylist.html :
12 | 13 | 14 | 25
And the skeleton mytest.html :
#CACHE{0}
<BOUCLE_art(ARTICLES){id_article IN #INCLURE{fond=mylist}|explode{'|'}}>
<h1>#TITRE</h1>
</BOUCLE_art>
The filter "explode is a php function that separates the string passed as a parameter and fills an array with values. The function takes as first parameter the character used for the separation ... and voila ! It is as if the included skeleton would pass an array parameter.
Application to the achievement of « impossible » loops
Here is an example to make a loop normally impossible with SPIP 1.9.2 : you want to list the articles in the sections marked with the keyword identifier 15.
One possibility offered by dynamic arrays would be to make the following skeleton (see also tools plugin SPIP Bonux to build dynamic arrays) :
#CACHE{0}
#SET{myarray, #ARRAY{}}
<BOUCLE_rub(RUBRIQUES){id_mot=15}>
[(#SET{myarray,
#GET{myarray}|push{#ID_RUBRIQUE}
})]
</BOUCLE_rub>
<BOUCLE_art(ARTICLES){id_rubrique IN #GET{myarray}|sinon{0}}{par titre}>
<h1>#TITRE</h1>
</BOUCLE_art>
This solution works perfectly, but it requires rewriting part of the code in each template where it is used.
Here is a solution where we share the first part :
We create a file list-rubriques.html which creates a list of identifiers separated by pipes.
<BOUCLE_rub(RUBRIQUES){id_mot?}{id_groupe?}{'|'}>#ID_RUBRIQUE</BOUCLE_rub>
You can then use this « sub-loop » in all the other skeletons. For example, it can be used for the following loop in another skeleton :
<BOUCLE_art(ARTICLES){id_rubrique IN #INCLURE{fond=list-rubriques}{id_mot=15}|explode{'|'}|sinon{0}}{par titre}>
<h1>#TITRE</h1>
</BOUCLE_art>
Compatibility :
Tested with SPIP 2.0.9, should also work with SPIP 1.9.2.
A (much) more efficient technique with SPIP 2.0.x
With version 2.0 came « automatic » joins (See Forcing joins on the site programmer.spip.org). We can then use the following loop to get the same result :
<BOUCLE_art(ARTICLES mots_rubriques){id_mot=15}{par titre}>
<h1>#TITRE</h1>
</BOUCLE_art>
The advantage is that :
- There are no inclusions
- All the joining work is done by the database, which is much faster.
Here is the request for evidence provided by the debug mode (var_mode=debug) :
SELECT articles.titre, articles.lang
FROM <span class="base64" title="PGNvZGUgY2xhc3M9InNwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lIiBkaXI9Imx0ciI+bWFiYXNlPC9jb2RlPg=="></span>.spip_articles
AS <span class="base64" title="PGNvZGUgY2xhc3M9InNwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lIiBkaXI9Imx0ciI+YXJ0aWNsZXM8L2NvZGU+"></span>
INNER JOIN <span class="base64" title="PGNvZGUgY2xhc3M9InNwaXBfY29kZSBzcGlwX2NvZGVfaW5saW5lIiBkaXI9Imx0ciI+bWFiYXNlPC9jb2RlPg=="></span>.spip_mots_rubriques
AS L1 ON ( L1.id_rubrique = articles.id_rubrique )
WHERE (articles.statut = 'publie')
AND (articles.date < '9999-12-31')
AND (L1.id_mot = 15)
GROUP BY articles.id_article
ORDER BY articles.titre
Aucune discussion
Ajouter un commentaire
Avant de faire part d’un problème sur un plugin X, merci de lire ce qui suit :
Merci d’avance pour les personnes qui vous aideront !
Par ailleurs, n’oubliez pas que les contributeurs et contributrices ont une vie en dehors de SPIP.
Suivre les commentaires : |