Views: 9704
Last Modified: 08.06.2015

Components can work with any templating engines that can be connected from PHP. In order to add a new templating motor onto a website it is necessary to determine (or expand) the global variable $arCustomTemplateEngines in the file /bitrix/php_interface/init.php. This variable contains an associative array where each element is similar to:

   "templator_code" => array(
      "templateExt" => array("extension1"[, "extension2"...]),
      "function" => "motor_connection_function_name"
   )

where:

  • templator_code - an arbitrary word that is unique for the website,
  • extensionN - the extension of the file that must be processed by this templating motor,
  • motor_connection_function_name - the name of the function that can be invoked if the component template has the indicated extension. The function can be located in the same file /bitrix/php_interface/init.php.

For example, if the use of Smarty is required on the website in addition to the standard templating motor (PHP), the following code must be added to the file /bitrix/php_interface/init.php:

   global $arCustomTemplateEngines;
   $arCustomTemplateEngines = array(
      "smarty" => array(
         "templateExt" => array("tpl"),
         "function" => "SmartyEngine"
      ),
   );

As a result, when a .tpl template is connected, the function SmartyEngine will start up instead of the standard motor PHP. SmartyEngine must connect theSmarty motor.

The syntaxis of motor connection functions is as follows:

   function  motor_connection_function_name ($templateFile, $arResult, $arParams, $arLangMessages, $templateFolder, $parentTemplateFolder, $template)

where:

  • $templateFile – path to the template file from the website root,
  • $arResult – array of results of the component operation,
  • $arParams – array of incoming parameters of the component,
  • $arLangMessages – array of language messages (translations) of the template,
  • $templateFolder – path to the template folder from the website root (if the template is not located in the folder, this variable is empty),
  • $parentTemplateFolder - path from the website root to the composite component folder as a part of which this component is connected (if the component is connected independently, this variable is empty),
  • $template – template object.

The code of the templating motor connection function depends on the motor to be connected.

Complete Example of Smarty Motor Connection Smarty

The following code must be added to the file /bitrix/php_interface/init.php:

global $arCustomTemplateEngines;
$arCustomTemplateEngines = array(
   "smarty" => array(
      "templateExt" => array("tpl"),
      "function" => "SmartyEngine"
   )
);

function SmartyEngine($templateFile, $arResult, $arParams, $arLangMessages, $templateFolder, $parentTemplateFolder, $template)
{
   if (!defined("SMARTY_DIR"))
      define("SMARTY_DIR", "/libs/");

   require_once('/libs/Smarty.class.php');

   $smarty = new Smarty;

   $smarty->compile_dir = "/templates_c/";
   $smarty->config_dir = "/configs/";
   $smarty->template_dir = "/templates/";
   $smarty->cache_dir = "/cache/";

   $smarty->compile_check = true;
   $smarty->debugging = false;

   $smarty->assign("arResult", $arResult);
   $smarty->assign("arParams", $arParams);
   $smarty->assign("MESS", $arLangMessages);
   $smarty->assign("templateFolder", $templateFolder);
   $smarty->assign("parentTemplateFolder", $parentTemplateFolder);

   $smarty->display($_SERVER["DOCUMENT_ROOT"].$templateFile);
}

The line <absolute path to Smarty motor> must be replaced everywhere with the absolute path to Smarty motor. More detailed information about the installation of the motor on a website is provided in the Smarty help system.

In the sample code, the Smarty motor is registered in the array $arCustomTemplateEngines. Parameters of the motor are initialized in the SmartyEngine function in accordance with system requirements (see the Smarty documentation). Then, the variables of component operation results, incoming parameters, language messages, etc. are transferred to Smarty. And, in the end, the Smarty template processing and displaying method is invoked.

Complete Example of XML/XSLT Motor Connection

The following code must be added to the fileл /bitrix/php_interface/init.php:

global $arCustomTemplateEngines;
$arCustomTemplateEngines = array(
   "xslt" => array(
      "templateExt" => array("xsl"),
      "function" => "XSLTEngine"
   ),
);

function CreateXMLFromArray($xDoc, $xNode, $ar)
{
   foreach($ar as $key=>$val)
   {
      if(!is_string($key) || strlen($key)<=0)
         $key = "value";

      $xElement = $xDoc->createElement($key);
      if(is_array($val))
      {
         CreateXMLFromArray($xDoc, $xElement, $val);
      }
      else
      {
         $xElement->appendChild($xDoc->createTextNode(iconv(SITE_CHARSET, "utf-8", $val)));
      }
      $xNode->appendChild($xElement);
   }
   return $xNode;
}

function XSLTEngine($templateFile, $arResult, $arParams, $arLangMessages, $templateFolder, $parentTemplateFolder, $template)
{
   $arResult["PARAMS"] = array(
      "templateFolder" => $templateFolder,
      "parentTemplateFolder" => $parentTemplateFolder,
      "arParams" => $arParams,
      "arLangMessages" => $arLangMessages
   );

   $xDoc = new DOMDocument("1.0", SITE_CHARSET);
   $xRoot = $xDoc->createElement('result');
   CreateXMLFromArray($xDoc, $xRoot, $arResult);
   $xDoc->appendChild($xRoot);

   $xXsl = new DOMDocument();
   $xXsl->load($_SERVER["DOCUMENT_ROOT"].$templateFile);

   $xProc = new XSLTProcessor;
   $xProc->importStyleSheet($xXsl);

   echo $xProc->transformToXML($xDoc);
}


Courses developed by Bitrix24