How to use an array in a template ?

Arrays are really usefull when you have to manipulate the same
elements several times.
They have been introduced with the version 1.92 :
#ARRAY{key1,value1,key2,value2...} allows the creation of a variable length table (useful for constructing a table for a #SET and/or for use with a dynamic « IN » criterion )"
This tag allows operations with arrays.

Arrays are really usefull when you have to manipulate the same
elements several times.

They have been introduced with the version 1.92 :
#ARRAY{key1,value1,key2,value2...} allows the creation of a variable
length table (useful for constructing a table for a #SET and/or for
use with a dynamic « IN » criterion )"

This tag allows you to do a lot of operations with the arrays.

Example

Here is an example taken from the plugin spipbb

#SET{modos,#ARRAY}
<BOUCLE_modo_art(AUTEURS){id_article}>
        #SET{modos,#GET{modos}|array_merge{#ARRAY{#COMPTEUR_BOUCLE,#ID_AUTEUR}}}
</BOUCLE_modo_art>

<BOUCLE_art_rub(ARTICLES){id_article}>
        <BOUCLE_rub_admin(AUTEURS_RUBRIQUES){id_auteur !IN
#GET{modos}}{id_rubrique=#ID_RUBRIQUE}>
                <BOUCLE_aut_admin(AUTEURS){id_auteur}>

#SET{modos,#GET{modos}|array_merge{#ARRAY{#COMPTEUR_BOUCLE,#_aut_admin:ID_AUTEUR}}}
                </BOUCLE_aut_admin>
        </BOUCLE_rub_admin>
</BOUCLE_art_rub>
  1. An empty table is created
  2. #ARRAY{#COMPTEUR_BOUCLE,#ID_AUTEUR} creates an array containing the author’s id at an index that isn’t used yet. The standard PHP function array_merge() finally does it’s job :
    this value is now stored on the last index of the array ’modos’.
  3. Another loop completes this array with the other authors of
    articles that are in the same section.

Note that the PHP function is used as a filter : it’s possible because
it’s first argument is the array to manipulate.

Of course all this can be done with PHP code inside the template. The
advantage of using the tag #ARRAY is that your template is better
managed by the cache.

Hope it helps,

L’auteur :
Gilles Vincent ( http://my.opera.com/tech-nova/blog )

Discussion

Aucune discussion

Ajouter un commentaire

Avant de faire part d’un problème sur un plugin X, merci de lire ce qui suit :

  • Désactiver tous les plugins que vous ne voulez pas tester afin de vous assurer que le bug vient bien du plugin X. Cela vous évitera d’écrire sur le forum d’une contribution qui n’est finalement pas en cause.
  • Cherchez et notez les numéros de version de tout ce qui est en place au moment du test :
    • version de SPIP, en bas de la partie privée
    • version du plugin testé et des éventuels plugins nécessités
    • version de PHP (exec=info en partie privée)
    • version de MySQL / SQLite
  • Si votre problème concerne la partie publique de votre site, donnez une URL où le bug est visible, pour que les gens puissent voir par eux-mêmes.
  • En cas de page blanche, merci d’activer l’affichage des erreurs, et d’indiquer ensuite l’erreur qui apparaît.

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.

Qui êtes-vous ?
[Se connecter]

Pour afficher votre trombine avec votre message, enregistrez-la d’abord sur gravatar.com (gratuit et indolore) et n’oubliez pas d’indiquer votre adresse e-mail ici.

Ajoutez votre commentaire ici

Ce champ accepte les raccourcis SPIP {{gras}} {italique} -*liste [texte->url] <quote> <code> et le code HTML <q> <del> <ins>. Pour créer des paragraphes, laissez simplement des lignes vides.

Ajouter un document

Suivre les commentaires : RSS 2.0 | Atom