[ar] [ast] [bg] [br] [ca] [co] [cpf] [cs] [da] [de] [en] [eo] [es] [eu] [fa] [fon] [fr] [gl] [id] [it] [ja] [lb] [nl] [oc] [pl] [pt] [ro] [ru] [sk] [sv] [tr] [uk] [vi] [zh] Espace de traduction


#SET and #GET

July 2010 — updated on : June 2010

All the versions of this article:

The #SET tag defines a variable that will be usable throughout the rest of the same template file.
The #GET is then used to retrieve the value of that variable.

#GET{variable} returns "value".

The variable defined by #SET may be a character string or an array.

-  A character string:
#SET{my_string, article from #NOM}

#GET{my_string} will display:
article from Jimmy

-  An array [1]:
#SET{my_array, #ARRAY{a,one,b,two,c,three,d,four}}

[(#GET{my_array}|foreach)] will display:

  • a => one
  • b => two
  • c => three
  • d => four

Advanced uses with text strings

-  A regular expression:

#SET{reg1, '(^.*/[^/]+[/])'}
#SET{reg2, '\.[\w-_]+?'}
[(#FICHIER|replace{#GET{reg2}}|replace{#GET{reg1}})]<hr />

is designed to retrieve and display the file name of a document, enclosed by its access path and its extension.
For example, if #FICHIER is IMG/pdf/Stats_Sejour_au_311207.pdf, we would be displaying Stats_Sejour_au_311207.

A key purpose in using the #SET and #GET tags for regular expressions is that it allows the usage of brackets ([ and ]) within the |replace filter.

-  The results returned by an INCLURE:

[(#SET{my_return_value, #INCLURE{fond=calculations}})]

Default values

If variable has not yet been defined with a #SET{variable, value} beforehand, it is possible to specify a default value when using the corresponding #GET tag:
#GET{variable, default_value} will return "default_value" if "variable" has not been defined previously.

Warning: this method does not assign "default_value" to the "variable".

Scope of the variable defined with a par #SET

The values defined by the#SET tag remain local to the template file in which they have been defined.

In addition, the value of a variable with a SET inside an included file can not be retrieved by the corresponding GET from within the file that includes the template file with that SET.


[1See also the article dedicated to #ARRAY.

Show the template of this page Site powered by SPIP | Translation area | Private area