K4:Environment Variable
From In-Portal Developers Guide
(→Структура: Quick Save) |
Current revision (23:14, 27 November 2010) (view source) m (K4:Переменная окружения moved to K4:Environment Variable) |
||
(11 intermediate revisions not shown.) | |||
Line 1: | Line 1: | ||
{{toc | category = Структура приложения | sortkey = 004.001}} | {{toc | category = Структура приложения | sortkey = 004.001}} | ||
<!-- Пишите ПОСЛЕ этой строки, пожалуйста. Ничего не стирать! --> | <!-- Пишите ПОСЛЕ этой строки, пожалуйста. Ничего не стирать! --> | ||
- | |||
- | |||
The Environment variable is a standard query variable called "<code>env</code>". Name of this variable is defined in [[K4:Константы#ENV_VAR_NAME|ENV_VAR_NAME]] constant. The purpose of <code>env</code> variable is to pass a large number of variables in a compact way through the website pages. In most cases <code>env</code> variable will carry the values to load the object from the [[K4:Работа с базой данных|database]] on the current page (т.е. [[K4:Unit Configs#Prefix|Prefix]] и <code>ID</code>), but usage is not limited by that. | The Environment variable is a standard query variable called "<code>env</code>". Name of this variable is defined in [[K4:Константы#ENV_VAR_NAME|ENV_VAR_NAME]] constant. The purpose of <code>env</code> variable is to pass a large number of variables in a compact way through the website pages. In most cases <code>env</code> variable will carry the values to load the object from the [[K4:Работа с базой данных|database]] on the current page (т.е. [[K4:Unit Configs#Prefix|Prefix]] и <code>ID</code>), but usage is not limited by that. | ||
- | |||
- | |||
Note that in case if in [[K4:Unit Configs|Configuration file]] of passed the [[K4:Unit Configs#Prefix|Prefix]] is allowed [[K4:Unit_Configs#AutoLoad|auto-loading]], then at the time of first call to that object of [[K4:Unit Configs#Prefix|Prefix]] and existence of object <code>ID</code> in the <code>env</code> variable then that object will be loaded automatically. | Note that in case if in [[K4:Unit Configs|Configuration file]] of passed the [[K4:Unit Configs#Prefix|Prefix]] is allowed [[K4:Unit_Configs#AutoLoad|auto-loading]], then at the time of first call to that object of [[K4:Unit Configs#Prefix|Prefix]] and existence of object <code>ID</code> in the <code>env</code> variable then that object will be loaded automatically. | ||
- | == | + | == Structure == |
- | + | The value contained in the environment variable has the following structure: | |
<session_id>-<template>:m<main_prefix_variables>[:<prefix[.special]>-<value1>[-<value2>...[-<valueN>]]] | <session_id>-<template>:m<main_prefix_variables>[:<prefix[.special]>-<value1>[-<value2>...[-<valueN>]]] | ||
- | + | For the above scheme, it can be seen that the environment variable is always made up of the following components: | |
- | * <code>session_id</code> - [[K4:UserSession#SessionKey| | + | |
- | * <code>template</code> - | + | * <code>session_id</code> - [[K4:UserSession#SessionKey|session identifier]] [[K4:PortalUser|user]]; |
- | * <code>main_prefix_variables</code> - | + | * <code>template</code> - path to the template, the rendering of which will be shown on the screen; |
+ | * <code>main_prefix_variables</code> - system variables, among which are [[K4:Theme#ThemeId|theme ID]] and [[K4:Language|language ID]], which will be used to output information on the page (will be explained later). | ||
- | + | Also, it may contain data from an unlimited number of [[K4:Unit Configs#Prefix|prefix]]es. Data sets from different [[K4:Unit Configs#Prefix|prefix]]es are separated with a colon ("<code>:</code>"). The first value in each set is the [[K4:Unit Configs#Prefix|prefix]] of the configuration file. If necessary, then the "<code>special</code>" can be indicated in the following format: "<code>prefix.special</code>". | |
- | + | The following is a list of parameters of the [[K4:Unit Configs#Prefix|prefix]] that can be passed through the environment variable, delimited using a hyphen ("<code>-</code>"). Parameter names and their order in a single set of a [[K4:Unit Configs#Prefix|prefix]] is set in the key [[K4:Unit Configs#QueryString|QueryString]] of its Configs|configuration file]]. For example: | |
<source lang="php"> | <source lang="php"> | ||
'QueryString' => Array( | 'QueryString' => Array( | ||
Line 30: | Line 27: | ||
</source> | </source> | ||
- | + | The above key value [[K4:Unit Configs#QueryString|QueryString]] is the standard for most [[K4:Unit Configs#Prefix|prefix]]es and therefore will be described below. | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Name || Description |
|- | |- | ||
- | | <code>id</code> || ID | + | | <code>id</code> || Object ID (<code>Item</code>) that can be used for [[K4:Unit Configs#AutoLoad|automatic loading]] of an object. |
|- | |- | ||
- | | <code>Page</code> || | + | | <code>Page</code> || Page number of the object list (<code>List</code>). |
|- | |- | ||
- | | <code>event</code> || [[EventHandler:About| | + | | <code>event</code> || [[EventHandler:About|Event]], that needs to be executed from a given [[K4:Unit Configs#Prefix|prefix]]. |
|- | |- | ||
| <code>mode</code> | | <code>mode</code> | ||
- | | | + | | Three types of values are possible: |
- | * "" - | + | * "" - values will be edited in the original table; |
- | * "<code>t</code>" - | + | * "<code>t</code>" - values will be edited in a temporary table in the main window; |
- | * "<code>t<wid></code>" - | + | * "<code>t<wid></code>" - values will be edited in a temporary table that was created as the result of a <code>popup</code>, where the <code>window id</code> is "<code><wid></code>". |
|} | |} | ||
- | {{InfoBox| | + | {{InfoBox|The values in this array are (<code>case-sensitive</code>).}} |
+ | |||
+ | == Getting Data from the Environment Variable == | ||
+ | If the URL used to open the site contains an environment variable, then its value will automatically be processed by the system. Direct access to the value of the environment variable isn't recommended. The result of processing '''each''' of the passed [[K4:Unit Configs#Prefix|prefixes]] creates variables, e.g. "<code>'''prefix[.special]_VariableName'''</code>". One variable will be created for each '''value''' in the [[K4:Unit Configs#QueryString|QueryString]] array of the passed [[K4:Unit Configs#Prefix|prefix]]. Variables created by this process can then be used just like any other variables passed in a request to the server (i.e. use the method <code>'''Application::GetVar'''</code>). This will be demonstrated in the below example. | ||
+ | * Variables defined in the [[K4:Unit Configs|configuration file]]: | ||
- | |||
- | |||
- | |||
<source lang="php"> | <source lang="php"> | ||
'Prefix' => 'sample-prefix', | 'Prefix' => 'sample-prefix', | ||
Line 60: | Line 58: | ||
</source> | </source> | ||
- | * | + | * Environment Variable Values: |
<source lang="xml">-template:m0--1--s-:sample\-prefix-15-testing</source> | <source lang="xml">-template:m0--1--s-:sample\-prefix-15-testing</source> | ||
- | + | There are two variables defined in the above example for the "<code>sample-prefix</code>" prefix in the [[K4:Unit Configs|configuration file]]: "<code>sample_variable</code>" and "<code>another_variable</code>". The values that are passed in the environment variable for this prefix are "<code>15</code>" and "<code>testing</code>", respectively. Two variables will be created after processing the environment variable for this prefix: | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Name || Value |
|- | |- | ||
| <code>sample-prefix_sample_variable</code> || <code>15</code> | | <code>sample-prefix_sample_variable</code> || <code>15</code> | ||
Line 71: | Line 69: | ||
| <code>sample-prefix_another_variable</code> || <code>testing</code> | | <code>sample-prefix_another_variable</code> || <code>testing</code> | ||
|} | |} | ||
- | {{TipBox| | + | {{TipBox|The variable will be created even if a value isn't passed.}} |
- | + | The following code will allow getting the value of either of the variables created above: | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! <code>PHP</code> Example || Template Example |
|- | |- | ||
| <source lang="php">$sample_variable = $this->Application->GetVar('sample-prefix_sample_variable');</source> | | <source lang="php">$sample_variable = $this->Application->GetVar('sample-prefix_sample_variable');</source> | ||
| <source lang="xml">value: <inp2:m_Get name="sample-prefix_sample_variable"/></source> | | <source lang="xml">value: <inp2:m_Get name="sample-prefix_sample_variable"/></source> | ||
|} | |} | ||
- | {{InfoBox| | + | {{InfoBox|To avoid hard-coding the value of the prefix, it can be obtained dynamically using the following methods:<code>'''kEvent::getPrefixSpecial()'''</code> (for [[EventHandler:About|events]]) and <code>'''TagProcessor:getPrefixSpecial()'''</code> (for [[TagProcessor:About|tags]]).}} |
- | == | + | == Building Links == |
- | + | Since the environment variable is used to compactly pass data between the pages of a site, the only way to to add something to it is by building a link. All links in K4 are built using the method "<code>'''Application::HREF'''</code>". For example, it's used in the method code>'''Application::Redirect'''</code> and in the [[TagProcessor:About|tags]] [[TagProcessor:m_Link|m_Link]], [[TagProcessor:st_ContentBlock|st_ContentBlock]], [[TagProcessor:lang_LanguageLink|lang_LanguageLink]] and [[TagProcessor:m_FormAction|m_FormAction]]. This method accepts the following four parameters. | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Parameter || Description |
|- | |- | ||
- | | {{ConfigProperty|$t|string}} || | + | | {{ConfigProperty|$t|string}} || Name of the template to which a link needs to be built (for example "<code>custom/tests/test_edit</code>"). This parameter is required, but if a blank value is passed, then the current template will be used (the one the user opened). |
|- | |- | ||
- | | {{ConfigProperty|$prefix|string}} || | + | | {{ConfigProperty|$prefix|string}} || This optional parameter is available to make it possible to build a link to the front end of the site while located in the administrative console. To do this, it's value has to be passed as "<code>_FRONT_END_</code>". {{InfoBox|Currently, this functionality doesn't work (see [[Question:How to tell the m Link tag to build a link to the Front-End?|this question]]) and to build a link to the front-end, "<code>index.php</code>" has to be passed as the value of the [[#index_file|index_file]] parameter.}} |
|- | |- | ||
- | | {{ConfigProperty|$params|array}} || | + | | {{ConfigProperty|$params|array}} || This is a set of parameters that should be passed in a link. In addition to passing general application parameters, it's also possible to pass many special purpose parameters, which are described in [[#Special Purpose Parameters|below]]. |
|- | |- | ||
- | | {{ConfigProperty|$index_file|string}} || | + | | {{ConfigProperty|$index_file|string}} || Optional name of the <code>php</code> file that should be used in the resulting link. By default, it's set to "<code>index.php</code>" (for the front-end) and "<code>admin/index.php</code>" (for the administrative console). |
|} | |} | ||
- | === | + | === Special Purpose Parameters === |
{| class="prettytable" | {| class="prettytable" | ||
- | | {{ConfigProperty|pass|string}} || | + | | {{ConfigProperty|pass|string}} || In this parameter are passed the names of the [[K4:Unit Configs#Prefix|prefix]] tags (comma delimited) that will be used for building the value of the environment variable in the resulting link. It's also possible to pass the value "<code>'''all'''</code>" so that all [[K4:Unit Configs#Prefix|prefix]]es in a link are used on the page of a site. For example, "<code>m,sample-prefix</code>" or "<code>all</code>". {{TipBox|'''Always''' when writing a list of prefixes, the first one must be the [[K4:Unit Configs#Prefix|prefix]] "<code>m</code>".}} |
|- | |- | ||
| {{ConfigProperty|index_file|string}} | | {{ConfigProperty|index_file|string}} | ||
- | | | + | | An alternative way to set the value of the "<code>index_file</code>" parameter for the <code>Application::HREF</code> method through the template. For example (if passing "<code>another_index.php</code>"): |
<source lang="xml"> | <source lang="xml"> | ||
- | http://www.sample-site.com/another_index.php?env=-template:m0--1--s- ( | + | http://www.sample-site.com/another_index.php?env=-template:m0--1--s- (with parameter) |
- | http://www.sample-site.com/index.php?env=-template:m0--1--s- ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s- (without parameter) |
</source> | </source> | ||
|- | |- | ||
| {{ConfigProperty|escape|int}} | | {{ConfigProperty|escape|int}} | ||
- | | | + | | If this parameter is defined, then the [http://php.prod.intechnic.lv/manual/en/function.addslashes.php addslashes] function will be applied to the resulting link. When using in a template, use "<code>'''js_escape'''</code>" instead of this parameter because it's the latest version of this parameter and will work for all tags. For example (if passing "<code>1</code>"): |
<source lang="xml"> | <source lang="xml"> | ||
- | http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\\-prefix-15-testing ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\\-prefix-15-testing (with parameter) |
- | http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\-prefix-15-testing ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\-prefix-15-testing (without parameter) |
</source> | </source> | ||
- | + | Usually the "<code>'''js_escape'''</code>" (in templates) is used to build links that are used in <code>JavaScript</code>. | |
|- | |- | ||
| {{ConfigProperty|anchor|string}} | | {{ConfigProperty|anchor|string}} | ||
- | | | + | | This parameter allows adding the value defined in it as an anchor in the resulting link. For example (passing "<code>sample_anchor</code>"): |
<source lang="xml"> | <source lang="xml"> | ||
- | http://www.sample-site.com/index.php?env=-template:m0--1--s-#sample_anchor ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s-#sample_anchor (with parameter) |
- | http://www.sample-site.com/index.php?env=-template:m0--1--s- ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s- (without parameter) |
</source> | </source> | ||
|- | |- | ||
| {{ConfigProperty|no_amp|int}} | | {{ConfigProperty|no_amp|int}} | ||
- | | | + | | If this parameter is passed, then all variables, used in the resulting link will be combined with the "<code>&</code>" symbol (compatible with <code>JavaScript</code>). In all other cases, the variables will be combined using the "<code>&amp;</code>" characters (compatible with <code>HTML</code>). For example, (if passing "<code>1</code>"): |
<source lang="xml"> | <source lang="xml"> | ||
- | http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 (with parameter) |
- | http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 ( | + | http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 (without parameter) |
</source> | </source> | ||
|} | |} | ||
- | === | + | === Using Passed Parameters === |
- | + | All passed parameters are divided into '''3 groups''': | |
- | * | + | * [[#Special use parameters|Special use]] parameters]]; |
- | * | + | * Parameters that will be used in the [[#Структура|environment variable]]; |
- | * | + | * All other parameters. |
- | + | A list of [[K4:Unit Configs#Prefix|prefix]]es is taken from the value of the [[#pass|pass]] parameter and a [[#Структура|fragment]] of the environment variable is set for each prefix that will represent it. If the value of a [[#Структура|variable's prefix]] isn't indicated in the parameter, the value received in the request to the server will be used or, if nothing was passed then a blank space will be used. Al parameters that '''were used''' in building the value of the environment variable are removed from the general list of parameters (to avoid them ending up in the resulting link). | |
- | + | The rest of the parameters, having not been used in the environment variable parameter (except [[#Special purpose parameters|special purpose]] parameters), are added to the resulting link using the line "<code>&amp;</code>" or "<code>&</code>" (if the special purpose parameter is used [[#no_amp|no_amp]]). | |
- | + | After the completion of the all of the above steps, the [[#Special purpose parameters|special purpose]] parameters are applied to the link. | |
- | == | + | == Recording Data == |
- | + | Recording values into the environment variable from templates results in creating the link, which the user will eventually follow. Formatting links from templates is done with the [[TagProcessor:m_Link|m_Link]] tag. The below examples demonstrates its use. | |
- | === | + | === Recording Data from a Template === |
<source lang="xml"> | <source lang="xml"> | ||
<a href="<inp2:m_Link template='cart' cart_event='OnAddProduct' pass='m,cart,product'/>">Add To Cart</a> | <a href="<inp2:m_Link template='cart' cart_event='OnAddProduct' pass='m,cart,product'/>">Add To Cart</a> | ||
</source> | </source> | ||
- | + | Below are descriptions of [[TagProcessor:m_Link|m_Link]] tag parameters, used in the above example. | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Parameter || Explanation |
|- | |- | ||
- | | {{ConfigProperty|template|string}} || | + | | {{ConfigProperty|template|string}} || Path to the template. On the front-end, this is the path relative to the [[K4:Theme|theme]] directory. |
|- | |- | ||
- | | {{ConfigProperty|cart_event|string}} || | + | | {{ConfigProperty|cart_event|string}} || The name of the [[EventHandler:About|event]] for the <code>cart</code> prefix. The indicated event will be executed only in the case where the <code>cart</code> prefix is set in the <code>pass</code> parameter. |
|- | |- | ||
- | | {{ConfigProperty|pass|string}} || | + | | {{ConfigProperty|pass|string}} || This parameter indicates the prefixes whose data must be passed in the environment variable. |
|} | |} | ||
- | === | + | === Recording Data from an Event === |
- | + | After successfully executing each [[EventHandler:About|event]], there is an automatic redirect to the template, from which the event was called. In order for the link that was built for the redirect to contain additional parameters, the method '''<code>kEvent:setRedirectParam</code>''' must be used. '''<code>kEvent:redirect</code>''' event will allow setting an alternative template to be used in the redirect link. This is demonstrated in the below example. | |
<source lang="php"> | <source lang="php"> | ||
Line 180: | Line 178: | ||
} | } | ||
</source> | </source> | ||
- | + | In this example, the value of the "<code>param_name</code>" variable will be available on the "<code>alternative_destination_template</code>" template. More information on getting the values of passed parameters is written in [[#Getting data from the Environment Variable|Above Heading]]. | |
- | == | + | == System Environment Variables == |
- | + | Apart from data from user [[K4:Unit Configs#Prefix|prefixes]], the "<code>m</code>" (<code>main</code>) prefix is always passed into the environment variable. It contains system environment variables. The prefix's configuration file is located in the "<code>core/units/general</code>" folder and is named "<code>general_config.php</code>" (name of the folder plus "<code>_config.php</code>"). There is a key ([[K4:Unit Configs#PortalStyleEnv|PortalStyleEnv]]) used in this configuration file, because of which the resulting environment variable for the prefix will not contain a dash ("<code>-</code>") between the name of the prefix and the value of its first variable (e.g. "<code>m5</code>" instead of the usual "<code>m-5</code>"). The following variables are passed with the help of this prefix: | |
{| class="prettytable" | {| class="prettytable" | ||
- | ! | + | ! Name || Description |
|- | |- | ||
- | | {{ConfigProperty|m_cat_id|int}} || [[K4:Category#CategoryId|ID | + | | {{ConfigProperty|m_cat_id|int}} || [[K4:Category#CategoryId|Category ID]], i.e. the category that the user is currently viewing. |
|- | |- | ||
- | | {{ConfigProperty|m_cat_page|int}} || | + | | {{ConfigProperty|m_cat_page|int}} || Page number in the categories list in the category indicated in the [[#m_cat_id|m_cat_id]] variable. |
|- | |- | ||
- | | {{ConfigProperty|m_lang|int}} || [[K4:Language#LanguageId|ID | + | | {{ConfigProperty|m_lang|int}} || [[K4:Language#LanguageId|Language ID]] in which the site needs to be shown (works the same way in the Administrative Console). If not set, then [[K4:Language#PrimaryLang|Primary Language ID]] will be used, set in the "<code>Configuration -> Regional</code>" [[K4:Добавление секций в дереве|section]]. |
|- | |- | ||
- | | {{ConfigProperty|m_theme|int}} || [[K4:Theme#ThemeId|ID | + | | {{ConfigProperty|m_theme|int}} || [[K4:Theme#ThemeId|Theme ID]] that needs to be used to show the front-end of the site. The value of this variable isn't used in the Administrative Console. If not set, then [[K4:Theme#PrimaryTheme|ID основной темы]] will be used, which is set in the "<code>Configuration -> Themes</code>" [[K4:Добавление секций в дереве|section]]. |
|- | |- | ||
| {{ConfigProperty|m_opener|int}} | | {{ConfigProperty|m_opener|int}} | ||
- | | | + | | This variable is used so that after "<code>Save</code>" is clicked (events [[EventHandler:OnSave|OnSave]], [[EventHandler:OnCreate|OnCreate]], [[EventHandler:OnUpdate|OnUpdate]]) and "<code>Cancel</code>" (event [[EventHandler:OnCancelEdit|OnCancelEdit]], [[EventHandler:OnCancel|OnCancel]]) in the edit form toolbar, so that the user is automatically returned to the template from which he came. For this, the array "<code>'''opener_stack_<m_wid>'''</code>" is used, which contains the templates that the user accessed before finally getting to the given template (for example "<code>Array ('users/user_list', 'users/user_edit_groups');</code>"). |
- | {{TipBox| | + | {{TipBox|The last element in this array will be the template that the user was on before the open template.}} |
- | + | This array is stored in the [[K4:SessionData|session]]. The value, passed in the variable will be taken as a command to change the contents of the "<code>'''opener_stack_<m_wid>'''</code>" array for the open window: | |
- | * <code>'''r''' ( | + | * <code>'''r''' (rest</code> - delete the array (used to build a link for the [[K4:Добавление секций в дереве|section]] tree); |
- | * <code>'''d''' (down)</code> - | + | * <code>'''d''' (down)</code> - add the open template to the array (used when switching to the editing template from a records list template); |
- | * <code>'''u''' (up)</code> - | + | * <code>'''u''' (up)</code> - delete the last template from the array (used when returning from the edit template to the grid/records list template); |
- | * <code>'''p''' (popup)</code> - | + | * <code>'''p''' (popup)</code> - add the open template to the array and create a new [[#m_wid|window ID]] (the same as "<code>'''d'''</code>", except that the edit form will be open in a popup window); |
- | * <code>'''s''' (stay)</code> - | + | * <code>'''s''' (stay)</code> - do nothing with the array (default value). |
|- | |- | ||
- | | {{ConfigProperty|m_wid|int}} || | + | | {{ConfigProperty|m_wid|int}} || Window ID that's only used for (<code>popups</code>). For main windows, the value is blank. The window ID is also used in formatting the name of the "<code>'''opener_stack_<m_wid>'''</code>" array, which is controlled through the value of the [[#m_opener|m_opener]] variable. |
|} | |} | ||
+ | |||
+ | [[en:{{FULLPAGENAME}}]] | ||
+ | [[ru:K4:Переменная окружения]] | ||
+ | |||
+ | [http://guide.in-portal.org/rus/index.php?title=K4:%D0%9F%D0%B5%D1%80%D0%B5%D0%BC%D0%B5%D0%BD%D0%BD%D0%B0%D1%8F_%D0%BE%D0%BA%D1%80%D1%83%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F&oldid=1065 Переменная окружения (revision 1065)] |
Current revision
| ||
---|---|---|
Статьи в этой категории | ||
|
The Environment variable is a standard query variable called "env
". Name of this variable is defined in ENV_VAR_NAME constant. The purpose of env
variable is to pass a large number of variables in a compact way through the website pages. In most cases env
variable will carry the values to load the object from the database on the current page (т.е. Prefix и ID
), but usage is not limited by that.
Note that in case if in Configuration file of passed the Prefix is allowed auto-loading, then at the time of first call to that object of Prefix and existence of object ID
in the env
variable then that object will be loaded automatically.
Contents |
Structure
The value contained in the environment variable has the following structure:
<session_id>-<template>:m<main_prefix_variables>[:<prefix[.special]>-<value1>[-<value2>...[-<valueN>]]]
For the above scheme, it can be seen that the environment variable is always made up of the following components:
-
session_id
- session identifier user; -
template
- path to the template, the rendering of which will be shown on the screen; -
main_prefix_variables
- system variables, among which are theme ID and language ID, which will be used to output information on the page (will be explained later).
Also, it may contain data from an unlimited number of prefixes. Data sets from different prefixes are separated with a colon (":
"). The first value in each set is the prefix of the configuration file. If necessary, then the "special
" can be indicated in the following format: "prefix.special
".
The following is a list of parameters of the prefix that can be passed through the environment variable, delimited using a hyphen ("-
"). Parameter names and their order in a single set of a prefix is set in the key QueryString of its Configs|configuration file]]. For example:
'QueryString' => Array( 1 => 'id', 2 => 'Page', 3 => 'event', 4 => 'mode', ),
The above key value QueryString is the standard for most prefixes and therefore will be described below.
Name | Description |
---|---|
id | Object ID (Item ) that can be used for automatic loading of an object.
|
Page | Page number of the object list (List ).
|
event | Event, that needs to be executed from a given prefix. |
mode
| Three types of values are possible:
|
Getting Data from the Environment Variable
If the URL used to open the site contains an environment variable, then its value will automatically be processed by the system. Direct access to the value of the environment variable isn't recommended. The result of processing each of the passed prefixes creates variables, e.g. "prefix[.special]_VariableName
". One variable will be created for each value in the QueryString array of the passed prefix. Variables created by this process can then be used just like any other variables passed in a request to the server (i.e. use the method Application::GetVar
). This will be demonstrated in the below example.
- Variables defined in the configuration file:
'Prefix' => 'sample-prefix', 'QueryString' => Array ( 1 => 'sample_variable', 2 => 'another_variable', ),
- Environment Variable Values:
-template:m0--1--s-:sample\-prefix-15-testing
There are two variables defined in the above example for the "sample-prefix
" prefix in the configuration file: "sample_variable
" and "another_variable
". The values that are passed in the environment variable for this prefix are "15
" and "testing
", respectively. Two variables will be created after processing the environment variable for this prefix:
Name | Value |
---|---|
sample-prefix_sample_variable | 15
|
sample-prefix_another_variable | testing
|
The following code will allow getting the value of either of the variables created above:
PHP Example | Template Example |
---|---|
$sample_variable = $this->Application->GetVar('sample-prefix_sample_variable'); | value: <inp2:m_Get name="sample-prefix_sample_variable"/> |
To avoid hard-coding the value of the prefix, it can be obtained dynamically using the following methods:kEvent::getPrefixSpecial() (for events) and TagProcessor:getPrefixSpecial() (for tags).
|
Building Links
Since the environment variable is used to compactly pass data between the pages of a site, the only way to to add something to it is by building a link. All links in K4 are built using the method "Application::HREF
". For example, it's used in the method code>Application::Redirect</code> and in the tags m_Link, st_ContentBlock, lang_LanguageLink and m_FormAction. This method accepts the following four parameters.
Parameter | Description | ||
---|---|---|---|
$t (string) | Name of the template to which a link needs to be built (for example "custom/tests/test_edit "). This parameter is required, but if a blank value is passed, then the current template will be used (the one the user opened).
| ||
$prefix (string) | This optional parameter is available to make it possible to build a link to the front end of the site while located in the administrative console. To do this, it's value has to be passed as "_FRONT_END_ ".
| ||
$params (array) | This is a set of parameters that should be passed in a link. In addition to passing general application parameters, it's also possible to pass many special purpose parameters, which are described in below. | ||
$index_file (string) | Optional name of the php file that should be used in the resulting link. By default, it's set to "index.php " (for the front-end) and "admin/index.php " (for the administrative console).
|
Special Purpose Parameters
pass (string) | In this parameter are passed the names of the prefix tags (comma delimited) that will be used for building the value of the environment variable in the resulting link. It's also possible to pass the value "all " so that all prefixes in a link are used on the page of a site. For example, "m,sample-prefix " or "all ".
| ||
index_file (string)
| An alternative way to set the value of the "index_file " parameter for the Application::HREF method through the template. For example (if passing "another_index.php "):
http://www.sample-site.com/another_index.php?env=-template:m0--1--s- (with parameter) http://www.sample-site.com/index.php?env=-template:m0--1--s- (without parameter) | ||
escape (int)
| If this parameter is defined, then the addslashes function will be applied to the resulting link. When using in a template, use "js_escape " instead of this parameter because it's the latest version of this parameter and will work for all tags. For example (if passing "1 "):
http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\\-prefix-15-testing (with parameter) http://www.sample-site.com/index.php?env=-template:m0--1--s-:sample\-prefix-15-testing (without parameter) Usually the " | ||
anchor (string)
| This parameter allows adding the value defined in it as an anchor in the resulting link. For example (passing "sample_anchor "):
http://www.sample-site.com/index.php?env=-template:m0--1--s-#sample_anchor (with parameter) http://www.sample-site.com/index.php?env=-template:m0--1--s- (without parameter) | ||
no_amp (int)
| If this parameter is passed, then all variables, used in the resulting link will be combined with the "& " symbol (compatible with JavaScript ). In all other cases, the variables will be combined using the "& " characters (compatible with HTML ). For example, (if passing "1 "):
http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 (with parameter) http://www.sample-site.com/index.php?env=-template:m0--1--s-&sample_parameter=value1&another_parameter=value2 (without parameter) |
Using Passed Parameters
All passed parameters are divided into 3 groups:
- Special use parameters]];
- Parameters that will be used in the environment variable;
- All other parameters.
A list of prefixes is taken from the value of the pass parameter and a fragment of the environment variable is set for each prefix that will represent it. If the value of a variable's prefix isn't indicated in the parameter, the value received in the request to the server will be used or, if nothing was passed then a blank space will be used. Al parameters that were used in building the value of the environment variable are removed from the general list of parameters (to avoid them ending up in the resulting link).
The rest of the parameters, having not been used in the environment variable parameter (except special purpose parameters), are added to the resulting link using the line "&
" or "&
" (if the special purpose parameter is used no_amp).
After the completion of the all of the above steps, the special purpose parameters are applied to the link.
Recording Data
Recording values into the environment variable from templates results in creating the link, which the user will eventually follow. Formatting links from templates is done with the m_Link tag. The below examples demonstrates its use.
Recording Data from a Template
<a href="<inp2:m_Link template='cart' cart_event='OnAddProduct' pass='m,cart,product'/>">Add To Cart</a>
Below are descriptions of m_Link tag parameters, used in the above example.
Parameter | Explanation |
---|---|
template (string) | Path to the template. On the front-end, this is the path relative to the theme directory. |
cart_event (string) | The name of the event for the cart prefix. The indicated event will be executed only in the case where the cart prefix is set in the pass parameter.
|
pass (string) | This parameter indicates the prefixes whose data must be passed in the environment variable. |
Recording Data from an Event
After successfully executing each event, there is an automatic redirect to the template, from which the event was called. In order for the link that was built for the redirect to contain additional parameters, the method kEvent:setRedirectParam
must be used. kEvent:redirect
event will allow setting an alternative template to be used in the redirect link. This is demonstrated in the below example.
function OnCreate(&$event) { parent::OnCreate($event); if ($event->status == erSUCCESS) { return ; } $event->redirect = 'alternative_destination_template'; $event->setRedirectParam('pass', 'm,test'); $event->setRedirectParam('param_name', 'param_value'); }
In this example, the value of the "param_name
" variable will be available on the "alternative_destination_template
" template. More information on getting the values of passed parameters is written in Above Heading.
System Environment Variables
Apart from data from user prefixes, the "m
" (main
) prefix is always passed into the environment variable. It contains system environment variables. The prefix's configuration file is located in the "core/units/general
" folder and is named "general_config.php
" (name of the folder plus "_config.php
"). There is a key (PortalStyleEnv) used in this configuration file, because of which the resulting environment variable for the prefix will not contain a dash ("-
") between the name of the prefix and the value of its first variable (e.g. "m5
" instead of the usual "m-5
"). The following variables are passed with the help of this prefix:
Name | Description |
---|---|
m_cat_id (int) | Category ID, i.e. the category that the user is currently viewing. |
m_cat_page (int) | Page number in the categories list in the category indicated in the m_cat_id variable. |
m_lang (int) | Language ID in which the site needs to be shown (works the same way in the Administrative Console). If not set, then Primary Language ID will be used, set in the "Configuration -> Regional " section.
|
m_theme (int) | Theme ID that needs to be used to show the front-end of the site. The value of this variable isn't used in the Administrative Console. If not set, then ID основной темы will be used, which is set in the "Configuration -> Themes " section.
|
m_opener (int)
| This variable is used so that after "Save " is clicked (events OnSave, OnCreate, OnUpdate) and "Cancel " (event OnCancelEdit, OnCancel) in the edit form toolbar, so that the user is automatically returned to the template from which he came. For this, the array "opener_stack_<m_wid> " is used, which contains the templates that the user accessed before finally getting to the given template (for example "Array ('users/user_list', 'users/user_edit_groups'); ").
This array is stored in the session. The value, passed in the variable will be taken as a command to change the contents of the "
|
m_wid (int) | Window ID that's only used for (popups ). For main windows, the value is blank. The window ID is also used in formatting the name of the "opener_stack_<m_wid> " array, which is controlled through the value of the m_opener variable.
|