Coding with SPIP 3.0
SPIP3.0 Technical Documentation

> > > > Analysing a template

Analysing a template

When SPIP’s parser analyses a template, it translates the syntax into a vocabulary known and understood by the compiler. We might then say that the parser is translating a particular language (the SPIP syntax), that we refer to as a "concrete syntax", into a precise language that we refer to as an "abstract syntax". This is defined by PHP objects in the ecrire/puclic/interfaces.php file.

With this page analysis, the parser creates a table describing it, sequentially and recursively, by using the vocabulary included by the compiler (the objects containing Text, Fields, Loops, Criteria, Placeholders, Includes, Polyglots).

To make things a little clearer, let’s look at what table is generated by a few template examples.

A text

Template :

  1. Simple text

Generated table: (output by a print_r)

  1. 0 =>
  2. Texte::__set_state(array(
  3. 'type' => 'texte',
  4. 'texte' => 'Simple text
  5. ',
  6. 'avant' => NULL,
  7. 'apres' => '',
  8. 'ligne' => 1,
  9. )),
  10. )

Download

The table specifies that the first element read on the page (key 0) is a "Texte" element, starting on line 1, and holding the text string "Simple text".

A tag

Template:

  1. [before(#VAL)after]

We can read from the generated table below, that the first element read on the page is a Field ("champ" in French) (a tag), that it’s name is "VAL", that it is not within a loop (otherwise the id_loop would be defined), and that what is in the optional section before the tag is a "Texte" element with the text string being "before".

Generated table:

  1. 0 =>
  2. Champ::__set_state(array(
  3. 'type' => 'champ',
  4. 'nom_champ' => 'VAL',
  5. 'nom_boucle' => '',
  6. 'avant' =>
  7. 0 =>
  8. Texte::__set_state(array(
  9. 'type' => 'texte',
  10. 'texte' => 'before',
  11. 'avant' => NULL,
  12. 'apres' => '',
  13. 'ligne' => 1,
  14. )),
  15. ),
  16. 'after' =>
  17. 0 =>
  18. Texte::__set_state(array(
  19. 'type' => 'texte',
  20. 'texte' => 'after',
  21. 'avant' => NULL,
  22. 'apres' => '',
  23. 'ligne' => 1,
  24. )),
  25. ),
  26. 'etoile' => '',
  27. 'param' =>
  28. ),
  29. 'fonctions' =>
  30. ),
  31. 'id_boucle' => NULL,
  32. 'boucles' => NULL,
  33. 'type_requete' => NULL,
  34. 'code' => NULL,
  35. 'interdire_scripts' => true,
  36. 'descr' =>
  37. ),
  38. 'ligne' => 1,
  39. )),
  40. 1 =>
  41. Texte::__set_state(array(
  42. 'type' => 'texte',
  43. 'texte' => '
  44. ',
  45. 'avant' => NULL,
  46. 'apres' => '',
  47. 'ligne' => 1,
  48. )),
  49. )

Download

A loop

Let’s look at one more example of a loop using a tag, which is a little more complicated since it implies a circular reference in the generated table. Look at this simple template segment:

Template:

  1. <BOUCLE_a(ARTICLES){id_article=3}>
  2. #TITRE
  3. </BOUCLE_a>

Download

This loop selects article 3 and should display the title of the article. The page table, if we were to try to display it, would end up generating a recursion error. The illustration shows that the second element read in the loop is a Field ("champ" in French) or tag named "TITRE". This field contains a reference to the loop which it is defined within ('boucles'=>array(...)). This loop contains the tag which belongs to the loop containing the tag which belongs to the loop ...

Excerpt of the generated table

  1. 0 =>
  2. Boucle::__set_state(array(
  3. 'type' => 'boucle',
  4. 'id_boucle' => '_a',
  5. 'id_parent' => '',
  6. 'avant' =>
  7. ),
  8. 'milieu' =>
  9. 0 =>
  10. Texte::__set_state(array(
  11. 'type' => 'texte',
  12. 'texte' => '
  13. ',
  14. 'avant' => NULL,
  15. 'apres' => '',
  16. 'ligne' => 1,
  17. )),
  18. 1 =>
  19. Champ::__set_state(array(
  20. 'type' => 'champ',
  21. 'nom_champ' => 'TITRE',
  22. 'nom_boucle' => '',
  23. 'avant' => NULL,
  24. 'apres' => NULL,
  25. 'etoile' => '',
  26. 'param' =>
  27. ),
  28. 'fonctions' =>
  29. ),
  30. 'id_boucle' => '_a',
  31. 'boucles' =>
  32. '_a' =>
  33. Boucle::__set_state(array(
  34. 'type' => 'boucle',
  35. 'id_boucle' => '_a',
  36. 'id_parent' => '',
  37. 'avant' =>
  38. ),
  39. 'milieu' =>
  40. 0 =>
  41. Texte::__set_state(array(
  42. 'type' => 'texte',
  43. 'texte' => '
  44. ',
  45. 'avant' => NULL,
  46. 'apres' => '',
  47. 'ligne' => 1,
  48. )),
  49. 1 =>
  50. Champ::__set_state(array(
  51. 'type' => 'champ',
  52. 'nom_champ' => 'TITRE',
  53. 'nom_boucle' => '',
  54. 'avant' => NULL,
  55. 'apres' => NULL,
  56. 'etoile' => '',
  57. 'param' =>
  58. ),
  59. 'fonctions' =>
  60. ),
  61. 'id_boucle' => '_a',
  62. 'boucles' =>
  63. '_a' =>
  64. Boucle::__set_state(array(
  65. ...

Download

Why use such references?

Quite simply because they are then used for calculating the tags. When a tag is calculated, a part of this table is passed as a parameter (the famous $p that we will meet often). This part simply relates to the tag’s properties. To retrieve properties from the enclosing loop, all that is required (thanks to these references) is to call the parameter $p->boucles[$p->id_boucle].

  • Author :
  • Published :
  • Updated : 12/05/17
  • Translations : English, français