Retrieving the object and id_object

This article will show how to retrieve the type (object) and identifier of a loop, so that they can be used in the calculations of a tag.

Static tags

With the parameters for the tag $p, it is very simple to retrieve both object and id_object :

function balise_DEMO($p){
    // take the name of the object's primary key to calculate its value
    $_id_objet = $p->boucles[$p->id_boucle]->primary;
    $id_objet = champ_sql($_id_objet, $p);
    $objet = $p->boucles[$p->id_boucle]->id_table;
    $p->code = "calculer_balise_DEMO('$objet', $id_objet)";
    return $p;
function calculer_balise_DEMO($objet, $id_objet){
    $objet = objet_type($objet);
    return "Objet : $objet, id_objet : $id_objet";

Note that there are two functions here. The first uses the description of the tag to retrieve the name of its parent loop and the name of the primary key, and requests to retrieve the value of the primary key using the champ_sql() function. Note: what is retrieved in the $id_object variable is a code which must be evaluated using PHP (which is no longer a numeric value).

Once these parameters have been retrieved, we then add a PHP code to be evaluated in the code generated by the template compilation (this code will be cached). This is what is added into $p->code. That code will then next be evaluated during the creation of the called page cache.

The calculer_balise_DEMO() function is then passed the two desired arguments and returns a text which displays them on the page.

    #DEMO<br />
    <hr />
    #DEMO<br />

This template then enables the result to be seen, the #DEMO tag is passed the various data depending on the context in which it is found:

Object : article, id_object : 128
Object : article, id_object : 7
Object : rubrique, id_object : 1
Object : rubrique, id_object : 2

Dynamic tags

For a dynamic tag, its operation even prevents the simple retrieval of the type and identifier of the loop in which it has been written.

Even so, when it is needed, for example in creation of CVT forms which modify their processes depending on the type of loop, it is necessary to pass the object type and current loop identifier to the _dyn() function (and consequently to CVT’s load, verify and process functions).

The call tocalculer_balise_dynamique() makes it possible to retrieve the compilation context elements. If we ask to retrieve ’id_article’, we will certainly get one from within an ARTICLES loop, but not if we are in a RUBRIQUES loop. To be more specific, when we request an ’id_article’ value, SPIP acts as if it is retrieving the result from an #ID_ARTICLE tag, so it then looks for the value in the closest loop, otherwise it looks in the context, and it also depends on the tags which have been specifically declared.

We could ask to calculate id_object quite easily, but object will require passing a tag returning the object value. This tag does not exist by default within SPIP 2.x, so it must be created with a (DEMODYN_OBJET), which gives us:

function balise_DEMODYN($p){
    // primary key
    $_id_objet = $p->boucles[$p->id_boucle]->primary;
    return calculer_balise_dynamique(
        $p, 'DEMODYN', array('DEMODYN_OBJET', $_id_objet)

function balise_DEMODYN_OBJET($p) {
    $objet = $p->boucles[$p->id_boucle]->id_table;
    $p->code = $objet ? objet_type($objet) : "balise_hors_boucle";
    return $p;  

function balise_DEMODYN_dyn($objet, $id_objet){
    return "Objet : $objet, id_objet : $id_objet";

Author Mark Baber Published : Updated : 02/06/10

Translations : English, français