Views: 6507 (Data available since 06.02.2017)
Component template is a program code that converts data prepared by a component directly to HTML code.
Component templates are subdivided into system and user:
- System templates come together with the component and are located in the subfolder templates of the component folder.
- User templates of the component are the templates that have been changed according to the requirements of a specific website. They must be located in the folders of website templates (i.e. in
/bitrix/templates/website_template/). When the component template is copied using the system it means that they will be located at the following path:
Component templates are defined by names. Default template is named .default. If no template name is indicated in the component parameter settings, the default template is retrieved. Other templates can have arbitrary names.
Component templates can be folders or files. If the template does not require translation into other languages, own styles, and other resources, such template may be located in a file. Otherwise, the template should be located in a folder.
Each component template is indivisible. If the system template of a component must be changed to fit for a specific website, such a component template must be copied to the website template folder in its integrity.
For example, the component bitrix:catalog.site.selector has the system template dropdown located in the subfolder templates of the component folder. If this component template is to be changed to fit a specific website, the dropdown template folder should be copied into the folder
/bitrix/templates/website_template/components/bitrix/catalog.site.selector/ and changed as one deems necessary.
When including a component into a web page, the administrator shall establish which display template, exactly, will be used in this specific case.
Variables Available in the Component Template
The following variables are used in the template:
- $templateFile – a path to the template from the website root, e.g.:
- $arResult – an array of component operation results.
- $arParams – an array of input parameters of a component; it may be used to keep track of the set parameters of the template (e.g., displaying full pictures or links).
- $arLangMessages – an array of language messages of the template (not required for php templates).
- $templateFolder - a path to the folder with the template from DOCUMENT_ROOT (e.g.,
- $parentTemplateFolder – a parent template folder. This variable is very convenient to connect additional images or scripts (resources). It should be introduced to form a full path from the template folder.
- $component — a link to the currently invoked component (CBitrixComponent type).
- $this - a link to the current template (object describing the template, CBitrixComponentTemplate type)
- $templateName — the name of the component template (e.g., .default)
- $componentPath - a path to the component from DOCUMENT_ROOT (e.g.,
- $templateData — an array for entry; please note that here the data from template.php can be transferred to the file component_epilog.php, and these data will be sent to cache because the file component_epilog.php is executed with each hit.
Also, within the PHP template, the variables $APPLICATION, $USER and $DB are declared global.
Simple Component Template
The file of a simple component template may contain the following subfolders and files:
/lang where the files of language messages (translations) of the component template are located;
- The file result_modifier.php that is to be connected directly before connecting the component template. This file receives as input the array of component operation results
$arResult and the array of component invocation parameters
$arParams. This way, it is possible, for example, to change the array of the component operation results to fit a specific template.
- The file style.css determines the styles required for this template.
- The file script.js determines and connects java scripts required for this template. This file may be absent.
- The file .description.php containing the name and description of the template for the visual editor.
- The file .parameters.php containing a description of additional input parameters of the template for the visual editor.
- The file template.ext is the template proper. The extension ext depends on the type of the templating motor to be connected. The default extension is php. This file is mandatory.
- Any other folders and files containing the resources required for the component template. For example, the image folder containing the images required by the template.
Composite Component Template
A composite component template contains the same folders as a simple component template, plus:
- Templates of simple components that form part of the composite template. These templates are located in the folders of the type
/namespace/simple_component_name/ in the composite component template folder.
- Simple components that form part of the composite template are connected in the templates of pages of the composite component.
How the System Searches for the Template
The following search algorithm is used to find a suitable template for a component:
- Everything begins with the folder
/bitrix/templates/current_site_template/components/. In this folder, the file or folder with the template name is searched for along the path
/component_namespace/component_name/. If there are none, then the availability of the file template_name.ext is searched for, where the ext extension includes all the available extensions of all templating motors established on the website that are checked one by one. If the template is found, the algorithm is ended.
- If no template is found on step 1, the search is performed in the folder
/bitrix/templates/.default/components/. The algorithm described in step 1 applies. If the template is found, the algorithm is ended.
- If no template is found in step 2, the search among the system templates (i.e. those supplied with the component) is performed.
Specifics of the search:
- If no template name is set, the template with the .default name is searched for.
- If the template is set as a folder name, then in case of a simple component in this folder the file template.extis searched for, and in case of a composite template - page_name.ext. The ext extension is assumed equal to php first, and then to extensions of other templating motors available on the website.
For example, the component bitrix:catalog.list must be shown using the template table. Let us assume that on the website, in addition to the standard templating motor (php files), the Smarty motor (tpl files) is also available. The system will first check the folder
/bitrix/templates/current_site_template/components/bitrix/catalog.list/ to find a file or a folder named table. If there are none, the system will check the aforementioned folder to find the files table.php and table.tpl. If nothing is found, the system will review the folders
f the component folder is found, the file template.php is searched for first and if no such file is found, the file template.tpl. is searched for. If the template is set as
table/template.php, the indicated file is retrieved at once.
If a simple component is retrieved as a part of a composite component, the simple component template is first searched for within the composite component template, and after that (if not found) in own templates. To make sure this rule works, when calling for simple components within composite components do not forget to indicate the variable $component as the fourth parameter indicating the parent component. I.e. the code for invoking a simple component must look as follows:
$APPLICATION->IncludeComponent("custom:catalog.element", "", array(...), $component);
The same folder (e.g.,
/bitrix/templates/current_site_template/components/) contains templates of two components, a composite and a simple:
- catalog (composite component that contains the simple catalog.section)
- catalog.section (simple)
According to website operation conditions, a single template must be used for two catalog.section
occurrences. In this case, this template must have a name other than .default
; otherwise, it will not be caught.
Only <namespace>, component name, and template name (and parameters of the component itself) must be indicated in the connection code. When processing the code, the core first checks the availability of the component template in the current website template:
/bitrix/templates/<site template name>/components/<namespace>/<component name>/<template name>/template.php.
If <namespace> is bitrix it is the folder for templates of standard components. If <namespace> is selected by you <name> for your components is located in
/bitrix/components/<name>, it is the folder for the templates of your components.
If no template file is available, the default website template is checked:
/bitrix/templates/.default/components/<namespace>/<component name>/<template name>/template.php.
Only after that will the component template be connected from the component folder.
The template is connected by the command:
$this->IncludeComponentTemplate($templatePage = "");
Where $templatePage is the name of the current page for a composite component, and an empty line for a simple component.
Component template connection examples:
- Let us connect the template of the current composite component for the Section page:
- Connect the template of the current simple component:
|Menu Component Template
|Start test ||<?if (!defined("B_PROLOG_INCLUDED")
|| B_PROLOG_INCLUDED!==true)die();?> |
| Script start ||<?if (!empty($arResult)):?> |
|<ul> tag opening — unnumbered list ||<ul class="…"> |
| Search cycle start ||<?foreach ($arResult as $arItem):?> |
| Link display ||<?if($arItem["SELECTED"]):?> |
| Active link ||<li><a href="<?=$arItem["LINK"]?>" class=
| Check for cycle continuation ||<?else:?> |
| Inactive link ||<li><a href="<?=$arItem["LINK"]?>">
| Link display ends ||<?endif?> |
| Search cycle ends ||<?endforeach?> |
| <ul> tag closes — unnumbered list ||</ul> |
|Script ends ||<?endif?> |