BITRIX/TWIG
Module for rendering twig templates in bitrix framework, (*1)
rus, (*2)
Intro
Bitrix framework allows render content with third-party template engines, in particular, twig., (*3)
To implement this, bitrix required a global (_sic!_) array $arCustomTemplateEngines
with next structure:, (*4)
$arCustomTemplateEngines['twig'] = [
'templateExt' => ['twig', 'html.twig'],
'function' => 'renderTwigTemplate'
];
The templateExt
key describes extensions of template files, and in function
key of this array contains the global (_sic!_) function name, which implement the render of template., (*5)
Also, function must instantiate the template engine class and echo (yes, fucking echo, not return!) the result of rendering., (*6)
If you even feel headache because of this, wait a minute, this is not all., (*7)
Installation
You MUST use the composer autoloader in application init. You site MUST use utf-8 codepage., (*8)
To install module, simple run, (*9)
composer require andrew72ru/bitrix-twig
Module will be stored in you vendor
, register it classes and functions., (*10)
Configuration
Default configuration:, (*11)
'debug' => false,
'charset' => 'utf-8',
'cache' => $_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/twig',
'auto_reload' => $this->request->get('clear_cache') ? 'Y' === strtoupper($this->request->get('clear_cache')) : false,
'autoescape' => false,
You may override or extend this configuration in bitrix/.settings.php
file. The configuration key is twigRenderer
. You config may be like this:, (*12)
// bitrix/.settings.php
<?php
return [
// skip
'twigRenderer' => [
'value' => [
'debug' => true,
]
],
// skip
];
Usage
Simple place the file <component_template_name>.thml.twig
(or <component_template_name>.twig
) to you template folder, instead of <component_template_name>.php
. All html-markup and twig functions / filters will be processed by twig engine. Module implement some custom functions to integrate template engine and bitrix framework (see below)., (*13)
Custom twig variables functions
Global variables:
-
$_SERVER
โ _SERVER
;
-
$_REQUEST
โ _REQUEST
;
-
$_GET
โ _GET
;
-
$_POST
โ _POST
;
-
$_FILES
โ _FILES
;
-
$_SESSION
โ _SESSION
;
-
$_COOKIE
โ _COOKIE
;
-
$_GLOBALS
โ _GLOBALS
Custom functions
showError
showMessage
showNote
bitrix_sessid_post
bitrix_sessid_get
getMessage
include_component
All of this the same bitrix framework functions., (*14)
-
_call_static($class, $method, $arguments = [])
is the function to call static method from available class from framework kernel or autoloaded classes.
Variables in template
-
result
โ $arResult
in traditional template;
-
params
โ $arParams
in traditional template;
-
lang
โ array with all loaded localized messages;
-
template
โ instance of CBitrixComponentTemplate
class;
-
templateFolder
โ path to template folder;
-
parentTemplateFolder
โ path to folder of parent template;
Events
The module call the onAfterTwigEngineInit
event after initialization. You may use it for add you own functions / filters / extensions., (*15)
Tests
Now the test contains a minimal (example) test, because of bitrix framework not have a psr autoloader, service container and other framework-must-have thinks. Tests are possible only if you have installed framework, installed module and at least one template, renders by this module., (*16)
If you know how to make test for all of classes โ you welcome., (*17)