Passing parameters to includes

You can pass one or more parameters to code segments that have been included in a template. By default, nothing is passed to included code except the processing date. To pass parameters to the compilation context of the template, they must be explicitly declared when calling the include:

  1. <INCLURE{fond=include_template}{parameter} />
  2. <INCLURE{fond=include_template}{parameter=value} />

Download

The first example with {parameter} only retrieves the value of #PARAMETER and passes it to the compilation context in the variable parameter. The second example assigns a specific value to that parameter variable. In both cases, within the included code, we can retrieve the value by reference using #ENV{parameter}.

Passing the entire current context

The {env} parameter can be used to pass the entire template compilation context to the code that is being included.

Example

  1. // file A.html
  2. <INCLURE{fond=B}{type}{key=# newt} />
  3. // file B.html
  4. <INCLURE{fond=C}{env}{colour=red} />
  5. // file C.html
  6. Type : #ENV{type} <br />
  7. Keyword : #ENV{key} <br />
  8. Colour : #ENV{colour}

Download

If we call the page spip.php?page=A&type=animal, that would pass the type and key parameters to the B.html template segment. This third example passes everything it has received and adds another parameter colour when it calls the C.html template segment.

Within the C.html template, we then see that it is possible to retrieve all of the parameters that have been passed.

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

Translations : English, français