Global keys

As you already know, HTML templates may contain system keys with predefined variables and local keys used in module scripts . In addition to these keys, templates may contain another type of keys with values of the global gVars parser array. This is a special array in which you can conveniently store key variables that can be used in many modules and templates. Access to this array is carried out through the parser pointer (by default, this is the $gParser variable), for example, in the page script you can define the id of the current section:

 $gParser->gVars['topic']['id'] = $hash['id'];

As you can see, this line forms the id variable for the topic array. Once the variable is defined, you can call it in any HTML template using the following keys: {TOPIC:ID} and [TOPIC:ID] . The key consists of two parts separated by a colon - TOPIC and ID, which corresponds to the array nesting: $gParser->gVars['topic']['id'] . The maximum nesting is up to the third level.

Important! In HTML templates, keys must always be specified in uppercase!

Another important aspect is the difference between keys in curly and square brackets. The fact is that the keys in curly brackets are replaced by data exactly the same as they were entered into the gVars array. The keys in square brackets are replaced by data that is translated into a safe representation (PHP function urlencode() ). For example:

 $gParser->gVars['topic']['psu'] = 'sport eqip';

Then in the template in place of the key:

  • {TOPIC:PSU} will be " sport eqip "
  • [TOPIC:PSU] will be " sport%20eqip "

Using keys in square brackets is necessary when calling modules, when keys act as input parameters of the module, or when the value needs to be inserted directly into JavaScript code or into an HTML form. For example:

 {MELBIS:melbis_goods_search([SEARCH:KEYWORD])}

or

 var search_key = '[SEARCH:KEYWORD]';

or

 <INPUT type=text name=search_key VALUE="[SEARCH:KEYWORD]";

Using the global gVars array, you can simplify calling many modules. For example, you can define the id key of the current section and use it to call all other modules (regardless of their nesting):

 {MELBIS:melbis_goods_in_topic([TOPIC:ID])}
 ...
 {MELBIS:melbis_action_in_topic([TOPIC:ID])}

If necessary, you can also access the global array in any module script. To do this, at the beginning of the module function, you must define the global parser variable, and then access (in the same way as in the page script) this array:

 function MELBIS_SHOP_START($mVars) { global $gParser; $id = $gParser->gVars['topic']['id']; }
Important! Although this option is available, accessing the global gVars array in module scripts should be done in extremely rare cases! It should be taken into account that the module caching system functions depending on their input parameters. Therefore, if you enable caching of a module in which you access a global variable directly, it will not work correctly . In addition, according to generally accepted rules, modules should be written in such a way that the result of their work depends only on the input parameters .