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

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

This form accepts SPIP shortcuts {{bold}} {italic} -*list [text->url] <quote> <code> and HTML code <q> <del> <ins>. To create paragraphs, just leave empty lines.

Add a document

Follow the comments: RSS 2.0 | Atom

updated on 25 June 2011