SPIP-Contrib

SPIP-Contrib

عربي | Deutsch | English | Español | français | italiano | Nederlands

286 Plugins, 197 contribs On SPIP-Zone, 220 visitors now

Home > Templates > Tutoriels pour squelettes > how to extend the capabilities of your loops

how to extend the capabilities of your loops

24 August 2009 – by Thierry Kauffmann

All the versions of this article: [English] [Español] [français]

1 vote

Here are given several techniques to extend the possibilities offered by SPIP standard loops :

First a technique to test the IN criterium of a loop with SPIP 1.9.2 or 2.0.x and also offering the possibility to write loops that seemed impossible to do.

Then a second technique available for SPIP 2.0.x, to make new loops with “automatic” or “forced” joins.

Ask for the moon to your loops :)!

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:

  1. <BOUCLE_art(ARTICLES){id_article IN 12,13,14,25}>
  2. <h1>#TITRE</h1>
  3. </BOUCLE_art>

Download

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:

  1. [(#SET{myarray,#ARRAY{0,12,1,13,2,14,3,25}})]
  2.  
  3. <BOUCLE_art1(ARTICLES){id_article IN #GET{myarray}|sinon{0}}{par hasard}>
  4. <h1>#TITRE</h1>
  5. </BOUCLE_art1>
  6.  
  7. <BOUCLE_art2(ARTICLES){id_article IN #GET{myarray}|sinon{0}}{par titre}>
  8. <h1>#TITRE</h1>
  9. </BOUCLE_art2>

Download

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:

  1. 12 | 13 | 14 | 25

And the skeleton mytest.html:

  1. #CACHE{0}
  2. <BOUCLE_art(ARTICLES){id_article IN #INCLURE{fond=mylist}|explode{'|'}}>
  3. <h1>#TITRE</h1>
  4. </BOUCLE_art>

Download

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 Bounce to build dynamic arrays):

  1. #CACHE{0}
  2.  
  3. #SET{myarray, #ARRAY{}}
  4. <BOUCLE_rub(RUBRIQUES){id_mot=15}>
  5. [(#SET{myarray,
  6. #GET{myarray}|push{#ID_RUBRIQUE}
  7. })]
  8. </BOUCLE_rub>
  9.  
  10. <BOUCLE_art(ARTICLES){id_rubrique IN #GET{myarray}|sinon{0}}{par titre}>
  11. <h1>#TITRE</h1>
  12. </BOUCLE_art>

Download

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.

  1. <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:

  1. <BOUCLE_art(ARTICLES){id_rubrique IN #INCLURE{fond=list-rubriques}{id_mot=15}|explode{'|'}|sinon{0}}{par titre}>
  2. <h1>#TITRE</h1>
  3. </BOUCLE_art>

Download

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:

  1. <BOUCLE_art(ARTICLES mots_rubriques){id_mot=15}{par titre}>
  2. <h1>#TITRE</h1>
  3. </BOUCLE_art>

Download

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):

  1. SELECT articles.titre, articles.lang
  2. FROM `mabase`.spip_articles
  3. AS `articles`
  4. INNER JOIN `mabase`.spip_mots_rubriques
  5. AS L1 ON ( L1.id_rubrique = articles.id_rubrique )
  6. WHERE (articles.statut = 'publie')
  7. AND (articles.date < '9999-12-31')
  8. AND (L1.id_mot = 15)
  9. GROUP BY articles.id_article
  10. ORDER BY articles.titre

Download

P.S.

  1. To use the new possibilities offered by SPIP 2.0.x, do not hesitate to use a tool like PhpMyAdmin, proposed in general by all hosters, to discover the tables and their structure (which is very clear !) to find the tables that will intermediate the new joints.
    Then check what sort of joins SPIP does by testing your desired skeleton with the parameter var_mode=debug and displaying the “calculation” generated by the loop in question. For that, you might want to learn some basics of SQL (Structured_Query_Language).
  2. (updated 2010-02-22). One might also read with interest this article dealing with the anti-duplicates. It is a very effective technique for creating groupings of various origins of the same type of object (the UNION in sql). This will prevent in most cases to resort to arrays.

updated on 21 August 2010

Back to top

Comment on this article

Who are you?
  • [Log in]

To show your avatar with your message, register it first on gravatar.com (free et painless) and don’t forget to indicate your Email addresse here.

Enter your comment here Les choses à faire avant de poser une question (Prolégomènes aux rapports de bugs. )
Add a document

Back to top

Here they're talking

  • (fr) Cycle2

    7 août 2014 – 104 commentaires

    La seconde version de la librairie Javascript « jQuery Cycle », qui permet de produire des diaporamas, se nomme « Cycle2 » : http://jquery.malsup.com/cycle2/. Voici cette nouvelle librairie mise en plugin pour SPIP. Si vous utilisez déjà « jQuery (...)

  • (fr) CleverMail

    20 janvier 2010 – 635 commentaires

    CleverMail est un plugin permettant d’envoyer des lettres d’informations à des abonnés depuis SPIP. Issu au départ d’un développement libre complètement indépendant de SPIP, il a connu une première version pour SPIP 1.9 avant d’être complètement réécris (...)

  • (fr) SPMC : Squelette par mot clé V2.0

    21 septembre 2009 – 48 commentaires

    Ce plugin prend la suite de squelette par mot clef V1.92 qui ne fonctionnait plus sous SPIP 2.0.x. Avec l’accord de Mortimer qui n’a plus le temps de le maintenir, je prend donc la suite. Présentation du plugin « Squelette par mot clé V2.0 » Le (...)

  • (fr) Sarka-SPIP 3

    15 septembre 2009 – 211 commentaires

    Si la lignée 3 de Sarka-SPIP a été l’occasion de refaire presque entièrement le code du squelette elle continue à évoluer et à s’améliorer au fil des versions. Nous ne saurions trop conseiller aux nouveaux utilisateurs - et aussi aux anciens - (...)

  • (fr) Paiement avec Formidable

    16 février 2015 – 53 commentaires

    Ce plugin « Paiement avec Formidable » permet d’ajouter une étape de paiement à la fin de la saisie d’un formulaire créé par le plugin Formidable. Il le complète et nécessite par ailleurs le plugin bank qui gère l’interface technique avec les prestataires (...)