Rus
Eng
Основные константы, функции и способы вывода в шаблонах Битрикс D7 и не только
Order site
By contacting me you will receive a reliable and knowledgeable contractor who will quickly and efficiently implement any task for you and your business.

All English-language materials have been translated fully automatically using the Google service

Section navigation:

Output of Bitrix scripts, styles and meta tags to header

 $ APPLICATION-> ShowTitle () // Display the page title

// Output meta tags
$ APPLICATION-> ShowMeta ("robots", false, false);
$ APPLICATION-> ShowMeta ("keywords", false, false);
$ APPLICATION-> ShowMeta ("description", false, false);

$ APPLICATION-> ShowLink ("canonical", null, false); // Canonical url
$ APPLICATION-> ShowLink ('prev', 'prev'); // rel = "prev"
$ APPLICATION-> ShowLink ('next', 'next'); // rel = "next"

$ APPLICATION-> SetAdditionalCSS (SITE_TEMPLATE_PATH. '/ Css / main.css'); // Output a separate css file
$ APPLICATION-> AddHeadString ('<link href = "'. SITE_TEMPLATE_PATH. '" / Css / common.min.css "type =" text / css "rel =" preload "/>', true);

$ APPLICATION-> ShowCSS (true, false); // Output css site styles

$ APPLICATION-> AddHeadScript (SITE_TEMPLATE_PATH. '/ Js / jquery.min.js'); // Output a single script

$ APPLICATION-> ShowHeadScripts (); // Output concatenated scripts

$ APPLICATION-> ShowHeadStrings (); // Output both scripts and styles added via the command $ APPLICATION-> AddHeadString

$ APPLICATION-> ShowHead (); // Output all service scripts and styles
$ APPLICATION-> ShowPanel (); // Display admin panel

$ APPLICATION-> SetPageProperty ('canonical', ($ APPLICATION-> IsHTTPS ()? 'Https: //': 'http: //'). 'Domain.ru'. $ APPLICATION-> GetCurPage ()); // Set the canonical link

if ($ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_TITLE']) {
$ APPLICATION-> SetTitle ($ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_TITLE']); // Set the page title
$ APPLICATION-> SetPageProperty ('title', $ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_TITLE']); // Set the title of the page
}

if ($ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_DESCRIPTION']) {
$ APPLICATION-> SetPageProperty ("description", $ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_DESCRIPTION']); // Set the meta description
}

if ($ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_KEYWORDS']) {
$ APPLICATION-> SetPageProperty ("keywords", $ arResult ['SECTION'] ['IPROPERTY_VALUES'] ['SECTION_META_KEYWORDS']); // Set meta keywords
}

// D7
use Bitrix \ Main \ Page \ Asset;

Asset :: getInstance () -> addJs (SITE_TEMPLATE_PATH. "/Js/jquery.js"); // Add a single script

Asset :: getInstance () -> addCss (SITE_TEMPLATE_PATH. "/Css/main.css"); // Add a single style
Asset :: getInstance () -> addString ('<link href = "'. SITE_TEMPLATE_PATH. '" / Css / common.min.css "; type =" text / css "rel =" preload "/>');

Asset :: getInstance () -> addString ("<link href = 'http: //fonts.googleapis.com/css? Family = PT + Sans: 400 & subset = cyrillic' rel = 'stylesheet' type = 'text / css' > "); // Add a single line to the header

When setting page properties, it is often necessary to get the property value into a variable in the header . The standard connection header.php in the template does not allow this, since the initialization of the session, properties, connections to the database occur after. A great hack comes to the rescue. Instead of

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");

Use this connection option:

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
$APPLICATION->SetPageProperty("mainImage", "background.jpg");
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_after.php");

Including scripts in the component template

  $ this-> addExternalCss ("/ local / styles.css");

$ this-> addExternalJS ("/ local / liba.js"); 

It is possible to directly call the component class

CBitrixComponent::includeComponentClass("tichiy:sale.order.ajax")

Connecting modules

  // Old version
CModule :: IncludeModule ("iblock");

// D7
use Bitrix \ Main \ Loader;

Loader :: includeModule ("iblock"); 

Working with module settings

  // Old version
COption :: SetOptionString ("main", "some_option", "option_value");
$ temp = COption :: GetOptionInt ("main", "some_option");
COption :: RemoveOption ("main", "some_option", "s1");

// D7
use Bitrix \ Main \ Config \ Option;

Option :: set ("main", "some_option", "option_value");
$ temp = Option :: get ("main", "some_option");
Option :: delete ("main", array (
    "name" => "some_option",
    "site_id" => "s1"
    )
); 

Localization

  // Old version
IncludeTemplateLangFile (__ FILE__);
echo GetMessage ("TEMPLATE_STRING");

// D7
use Bitrix \ Main \ Localization \ Loc;

Loc :: loadMessages (__ FILE__);
echo Loc :: getMessage ("TEMPLATE_STRING"); 

Caching

  // Old version
$ cache = new CPHPCache ();
if ($ cache-> InitCache ($ cacheTime, $ cacheId, $ cacheDir))
{
    $ result = $ cache-> GetVars ();
}
elseif ($ cache-> StartDataCache ())
{
    $ result = array ();
    // ...
    if ($ isInvalid)
    {
        $ cache-> AbortDataCache ();
    }
    // ...
    $ cache-> EndDataCache ($ result);
}

// D7
$ cache = Bitrix \ Main \ Data \ Cache :: createInstance ();
if ($ cache-> initCache ($ cacheTime, $ cacheId, $ cacheDir))
{
    $ result = $ cache-> getVars ();
}
elseif ($ cache-> startDataCache ())
{
    $ result = array ();
    // ...
    if ($ isInvalid)
    {
        $ cache-> abortDataCache ();
    }
    // ...
    $ cache-> endDataCache ($ result);
} 

Events

  // Old school
$ handler = AddEventHandler ("main",
    "OnUserLoginExternal",
    array (
        "Intervolga \\ Test \\ EventHandlers \\ Main",
        "onUserLoginExternal"
    )
);
RemoveEventHandler (
    "main",
    "OnUserLoginExternal",
    $ handler
);
RegisterModuleDependences (
    "main",
    "OnProlog",
    $ this-> MODULE_ID,
    "Intervolga \\ Test \\ EventHandlers",
    "onProlog"
);
UnRegisterModuleDependences (
    "main",
    "OnProlog",
    $ this-> MODULE_ID,
    "Intervolga \\ Test \\ EventHandlers",
    "onProlog"
);

$ handlers = GetModuleEvents ("main", "OnProlog", true);

// D7
use Bitrix \ Main \ EventManager;

$ handler = EventManager :: getInstance () -> addEventHandler (
    "main",
    "OnUserLoginExternal",
    array (
        "Intervolga \\ Test \\ EventHandlers \\ Main",
        "onUserLoginExternal"
    )
);
EventManager :: getInstance () -> removeEventHandler (
    "main",
    "OnUserLoginExternal",
    $ handler
);
EventManager :: getInstance () -> registerEventHandler (
    "main",
    "OnProlog",
    $ this-> MODULE_ID,
    "Intervolga \\ Test \\ EventHandlers",
    "onProlog"
);
EventManager :: getInstance () -> unRegisterEventHandler (
    "main",
    "OnProlog",
    $ this-> MODULE_ID,
    "Intervolga \\ Test \\ EventHandlers",
    "onProlog"
);
$ handlers = EventManager :: getInstance () -> findEventHandlers ("main", "OnProlog"); 

Working with files

  // Old school
CheckDirPath ($ _ SERVER ["DOCUMENT_ROOT"]. "/ Foo / bar / baz /");
RewriteFile (
    $ _SERVER ["DOCUMENT_ROOT"]. "/foo/bar/baz/1.txt",
    "hello from old school!"
);
DeleteDirFilesEx ("/ foo / bar / baz /");

// D7
use Bitrix \ Main \ Application;
use Bitrix \ Main \ IO \ Directory;
use Bitrix \ Main \ IO \ File;

Directory :: createDirectory (
    Application :: getDocumentRoot (). "/ foo / bar / baz /"
);
File :: putFileContents (
Application :: getDocumentRoot (). "/foo/bar/baz/1.txt",
    "hello from D7"
);
Directory :: deleteDirectory (
    Application :: getDocumentRoot (). "/ foo / bar / baz /"
); 

Error handling

  // Old version
global $ APPLICATION;
$ APPLICATION-> ResetException ();
$ APPLICATION-> ThrowException ("Error");
// ...
if ($ exception = $ APPLICATION-> GetException ())
{
    echo $ exception-> GetString ();
}

// D7
use Bitrix \ Main \ SystemException;

try
{
    // ...
    throw new SystemException ("Error");
}
catch (SystemException $ exception)
{
    echo $ exception-> getMessage ();
} 

Debugging

  // Old version
define ("LOG_FILENAME", $ _SERVER ["DOCUMENT_ROOT"]. "/ log24342.txt");

AddMessage2Log ($ _ SERVER);
echo "
". mydump ($ _ SERVER). "
"; // D7 // Write debug information to file Bitrix \ Main \ Diag \ Debug :: writeToFile (array ('ID' => $ id, 'fields' => $ fields), "", "/ logs / logname.log"); Bitrix \ Main \ Diag \ Debug :: dumpToFile (array ('ID' => $ id, 'fields' => $ fields), "", "/ logs / logname.log"); // Script execution time // At the beginning of the investigated section of the code, add: Bitrix \ Main \ Diag \ Debug :: startTimeLabel ('test 1'); // to the end: Bitrix \ Main \ Diag \ Debug :: endTimeLabel ('test 1'); // And for output we use: Bitrix \ Main \ Diag \ Debug :: getTimeLabels (); // Get the current timestamp Bitrix \ Main \ Diag \ Helper :: getCurrentMicrotime (); // Get the function call stack Bitrix \ Main \ Diag \ Helper :: getBackTrace ($ limit = 0, $ options = null);

Settings for error logging in the new kernel in .settings.php

'exception_handling' => array (
  'value' => array (
      'debug' => false, //Enable/disable debugging
      'handled_errors_types' => E_ALL & ~E_NOTICE & ~E_STRICT & ~E_WARNING, //The key specifies the types of errors that the system catches (does not ignore)
      'exception_errors_types' => E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_COMPILE_WARNING, //The key specifies the types of errors in which the system throws an exception.
      'ignore_silence' => true,
      'assertion_throws_exception' => true,
      'assertion_error_type' => 256,
      'log' => array (
          'settings' => array (
				'file' => "bitrix/logs/Errors_" . date("Y_m_d") . ".log",
				'log_size' => 1000000, // ~ file size limit 1Mb
          ),
      ),
  ),
  'readonly' => true,
),

To debug agents, you can use the constant BX_AGENTS_LOG_FUNCTION

define('BX_AGENTS_LOG_FUNCTION', 'TestAgents');

function TestAgents($agent, $operation, $result=false, $return=false){
    clearstatcache();
    $handle = fopen($_SERVER["DOCUMENT_ROOT"]."/agents.txt", "a");
    static $log = array();
    $time = date('Y-m-d-H:i:s');
    if($operation=='start'){
        $log[$agent['ID']] = microtime(true);      
        fwrite($handle, $_SERVER["REQUEST_TIME_FLOAT"].'-'.$time.'-'.$agent['ID'].'-start: '.$agent['NAME'].' ['.$agent['MODULE_ID'].'], '.$agent['AGENT_INTERVAL']."\n");
    }elseif($operation=='finish'){
        fwrite($handle, $_SERVER["REQUEST_TIME_FLOAT"].'-'.$time.'-'.$agent['ID'].'-finish ('.number_format(microtime(true) - $log[$agent['ID']], 4, '.', ' ').'s): '.$agent['NAME'].' ['.$agent['MODULE_ID'].'], '.$agent['AGENT_INTERVAL']."\n");
    }

    fclose($handle);
}

Sending Mail

  // Old version
CEvent :: Send (
    "NEW_USER",
    "s1",
    array (
        "EMAIL" => "info@site.ru",
        "USER_ID" => 42
    )
);

// D7
use Bitrix \ Main \ Mail \ Event;
Event :: send (array (
    "EVENT_NAME" => "NEW_USER",
    "LID" => "s1",
    "C_FIELDS" => array (
        "EMAIL" => "info@site.ru",
        "USER_ID" => 42
    ),
)); 

Working with GET- and POST

  // Old version
$ name = $ _POST ["name"];
$ email = htmlspecialchars ($ _ GET ["email"]);

// D7
use Bitrix \ Main \ Application;
$ request = Application :: getInstance () -> getContext () -> getRequest ();

$ name = $ request-> getPost ("name");
$ email = htmlspecialchars ($ request-> getQuery ("email")); 

Working with cookies

  // Old version
global $ APPLICATION;
$ APPLICATION-> set_cookie ("TEST", 42, false, "/", "example.com");
// Cookie will only be available on the next hit!
echo $ APPLICATION-> get_cookie ("TEST");

// D7
use Bitrix \ Main \ Application;
use Bitrix \ Main \ Web \ Cookie;
$ cookie = new Cookie ("TEST", 42);
$ cookie-> setDomain ("example.com");
Application :: getInstance () -> getContext () -> getResponse () -> addCookie ($ cookie);
// Cookie will only be available on the next hit!
echo Application :: getInstance () -> getContext () -> getRequest () -> getCookie ("TEST");

// Setting cookie via ajax with only prologue_before connection
$ application = Application :: getInstance ();
$ context = $ application-> getContext ();

$ cookie = new Cookie ("TEST", 12, time () + 60 * 60 * 24 * 60);
$ cookie-> setDomain ($ context-> getServer () -> getHttpHost ());
$ cookie-> setHttpOnly (false);

$ context-> getResponse () -> addCookie ($ cookie);
$ context-> getResponse () -> flush (""); 
  // Old version
global $ APPLICATION;
$ redirect = $ APPLICATION-> GetCurPageParam ("foo = bar", array ("baz"));

// D7
use Bitrix \ Main \ Application;
use Bitrix \ Main \ Web \ Uri;

$ request = Application :: getInstance () -> getContext () -> getRequest ();
$ uriString = $ request-> getRequestUri ();
$ uri = new Uri ($ uriString);
$ uri-> deleteParams (array ("baz"));
$ uri-> addParams (array ("foo" => "bar"));
$ redirect = $ uri-> getUri (); 

Working with the database

  // Old version
global $ DB;
$ record = $ DB-> Query ("select 1 + 1;") -> Fetch ();
AddMessage2Log ($ record);

// D7
use Bitrix \ Main \ Application;
use Bitrix \ Main \ Diag \ Debug;

$ record = Application :: getConnection ()
-> query ("select 1 + 1;")
-> fetch ();
Debug :: writeToFile ($ record); 

Include scopes

 // Connect in php mode
$ APPLICATION-> IncludeFile (SITE_DIR. "Include / flash.php", Array (), Array ("MODE" => "php", "NAME" => 'Shipping and payment'));

// Connect in html mode
$ APPLICATION-> IncludeFile (SITE_DIR. "Include / flash.php", Array (), Array ("MODE" => "html", "NAME" => 'Shipping and payment')); 

Connecting web messenger

 $ APPLICATION-> IncludeComponent ("bitrix: im.messenger", "", Array (), null, array ("HIDE_ICONS" => "N")); 

Working with infoblocks

 // Display the name, description and picture of the infoblock
$ cntIBLOCK_List = 19; // infoblock ID

$ name = CIBlock :: GetArrayByID ($ cntIBLOCK_List, "NAME"); // Information block name
$ description = CIBlock :: GetArrayByID ($ cntIBLOCK_List, "DESCRIPTION"); // Description of the info block
$ picture = CFile :: GetPath (CIBlock :: GetArrayByID ($ cntIBLOCK_List, "PICTURE")); // Information block image

CIBlockSection :: GetCount (array ("IBLOCK_TYPE" => "INFO_AFW", 'IBLOCK_ID' => 18, 'SECTION_ID' => $ arResult ["VARIABLES"] ["SECTION_ID"])) // Get the number of section subsections 

Constants

 SITE_TEMPLATE_PATH // Path to the active template from the site root

SITE_ID // ID of the active site

SITE_DIR // Site folder

LANGUAGE_ID // Current language

Session

 // Output the session identifier in the form
echo bitrix_sessid_post ();

// Get session id in javascript
BX.bitrix_sessid () 

Checks

 // Check for the administrative part
if (defined ('ADMIN_SECTION') && (ADMIN_SECTION === true))
  return false; 

Sample files

Generating 404 error

 Bitrix \ Iblock \ Component \ Tools :: process404 (
'Not found', // Message
true, // Whether it is necessary to define a 404th constant
true, // Whether to set the status
true, // Whether to show the 404th page
false // Link to a non-standard 404
); 

Transferring data from template.php or component_epilog.php to script

Code in component_epilog.php

 BX.message ({
FREE_BUTTON: '<? echo $ button; ?> '
}); 

Code in script.js

BX.message('FREE_BUTTON')

Connecting Bitrix core files:

 // Connect the header
require_once ($ _ SERVER ["DOCUMENT_ROOT"]. "/ bitrix / header.php");
// Connect the footer
require_once ($ _ SERVER ["DOCUMENT_ROOT"]. "/ bitrix / footer.php");
 require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_before.php');
require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_after.php');
 require_once ($ _ SERVER ["DOCUMENT_ROOT"]. "/ bitrix / modules / main / include / prolog_admin_before.php");
require_once ($ _ SERVER ["DOCUMENT_ROOT"]. "/ bitrix / modules / main / include / epilog_admin.php");

Passing $ arResult to component_epilog

Add

to the end of the tamplate.php file
 // add $ arResult to the cached result
if (property_exists ($ component, 'arResultCacheKeys')) {
    if (! is_array ($ component-> arResultCacheKeys)) {
        $ component-> arResultCacheKeys = array ();
    }
    $ sVarName = 'arResult';
    $ component-> arResultCacheKeys [] = $ sVarName;
    $ component-> arResult [$ sVarName] = $$ sVarName;
} 

Add to the beginning of the component_epilog.php file.

 // replace $ arResult of the epilogue with the value stored in the template
if (isset ($ arResult ['arResult'])) {
    $ arResult = & $ arResult ['arResult'];
    // include the language file
    global $ MESS;
    include_once (GetLangFileName (dirname (__ FILE __). '/ lang /', '/template.php'));
} else {
    return;
} 

Passing data from result_modifier.php to component_epilog

At the end of the file result_modifier.php add:

$this->__component->arResult["SIMILAR_PRODUCTS"] = $SECTIONS['SECTIONS'];
$this->__component->SetResultCacheKeys(array("SIMILAR_PRODUCTS"));

In component_epilog we simply get the key: $arResult['SIMILAR_PRODUCTS']

Add CNC to getlist

Sometimes it is necessary via getList () a directory element along with the correct element reference. The built-in function SetUrlTemplates comes to the rescue.

 $ rs = CIBlockElement :: GetList (
Array ("RAND" => "ASC"),
Array ("IBLOCK_ID" => 15, "IBLOCK_TYPE" => 'catalog'),
false,
Array ("nTopCount" => 4)
);
$ rs-> SetUrlTemplates ("/ shop / # SECTION_CODE # / # ELEMENT_CODE # /"); // Here we write the CNC template
while ($ row = $ rs-> GetNext ())
{
// Data output
} 

Get bxajaxid for ajax requests

bxajaxid is generated dynamically, and therefore it must be received separately in each component

 // Get bxajaxid for component \ template
$ bxajaxid = CAjax :: GetComponentID ('bitrix: system.auth .form', 'personal');

// Get bxajaxid in the php component <7
$ bxajaxid = CAjax :: GetComponentID ($ component -> __ name, $ component -> __ template -> __ name);

// Get bxajaxid in the component on php> 7
$ bxajaxid = CAjax :: GetComponentID ($ component -> __ name, $ component -> __ template -> __ name, $ component-> arParams ['AJAX_OPTION_ADDITIONAL']);

// If you get the error "Too few arguments to function CAjax :: GetComponentID ()" then try the following
CAjax :: GetComponentID ($ component -> __ name, $ component -> __ template -> __ name, '');

Passing fields from result_modifier to component_epilog

Add to result_modifier.php

$item = \Bitrix\Iblock\ElementTable::getList(array(
    "select" => array("ID", "PREVIEW_TEXT", "DETAIL_TEXT", "PREVIEW_PICTURE", "DETAIL_PICTURE"),
    "filter" => array("IBLOCK_ID" => Stroy\Data\Config::getCatalogIB(), "ID" => $arResult['ID']),
    "order"  => array("ID" => "ASC")
))->fetch();

$cp = $this->__component;

if (is_object($cp)){
    $cp->arResult['CUSTOM'] = $item;
    $cp->SetResultCacheKeys(array('CUSTOM'));

}

In component_epilog.php this data can be obtained in a variable

$arResult['CUSTOM']

Useful links

Working with the Bitrix system

Amazing Bitrix Repository

Useful tools for those who are in the same boat with Bitrix

Based on materials:

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Please message me to discuss the task. I work remotely and officially (as a self-employed professional); my hourly rate is 1,800 RUB, and I can dedicate 3–4 hours a day to the project. I respond quickly, and the consultation is free.

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site