Views: 5513
Last Modified: 10.10.2012

A generic component declaration

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?> 
<?
 // Verify and initialize input parameters
 if (the cache contains valid component output) 
 { 
 // Render cached data
 } 
 else 
 { 
 // Query data and create $arResult array according to structure
 // described in the component help
 $this->IncludeComponentTemplate(); 
 // Cache output here
 } 
?>

Adding a component language message file

The system automatically includes the language message files for a component and all the standard files (component.php, .description.php, .parameters.php). In other files that exist in a component, you can include language message files using the following call:

$this->IncludeComponentLang($relativePath = "", $lang = False)

here:
$relativePath – the path to a file relative to the component folder;
$lang – the language. A special value of False specifies the current language.

Example:

   $this->IncludeComponentLang("myfile.php");


Applying a component template

A template can be connected using the call:

$this->IncludeComponentTemplate($templatePage = "");

here, $templatePage is a current page name for a composite component, or an empty string for a simple component.

The following examples illustrate the inclusion of a component template:

1. Include the template of a current composite component for the section page:

   $this->IncludeComponentTemplate("section");

2. Include the template of a current simple component:

   $this->IncludeComponentTemplate();

Inside the component, the following predefined variables are available:

$templateFile – the path to the template relative to the site root;
$arResult – an array of the component return values;
$arParams – an array of the component input parameters;
$arLangMessages – an array of the template language dependent messages (undefined for PHP templates);
$templateFolder – the template folder, if the template resides in a folder (that contains additional resources);
$parentTemplateFolder – the parent template folder;
$component – the current component instance.

A PHP template can access the $APPLICATION, $USER, $DB global variables.


Additional methods available in components and templates

string CComponentEngine::MakePathFromTemplate($pageTemplate, $arParams);

here:
$pageTemplate – a page template in the following format: /catalog/#IBLOCK_ID#/section/#SECTION_ID#.php or catalog.php?BID=#IBLOCK_ID#&SID=#SECTION_ID#;
$arParams – an associated parameter mapping array. Here, keys are the parameter names and values are the parameter value.

Returns the path based on the $pageTemplate path template and the mapping array.

Example:

$url = CComponentEngine::MakePathFromTemplate(
"/catalog/#IBLOCK_ID#/section/#SECTION_ID#.php", 
        array( 
             "IBLOCK_ID" => 21, 
             "SECTION_ID" => 452  
             ) 
);

Establishing explicit links between subcomponents in a composite component

You can link the subcomponents using the return values and input parameters of the components.

If you need to pass data from a component comp1 to comp2, add the following instruction to the end of the component comp1 code:
return data;

Connect the component comp1 in the following way:

$result = $APPLICATION->IncludeComponent(comp1, ...);

Now, the data are in the $result variable and they can be passed to the comp2 component as input parameters.



Courses developed by Bitrix24