Views: 13511
Last Modified: 04.10.2024

Deferred functions are a process technique that permits to set the page header, navigation chain points, CSS styles, additional buttons in the control panel, meta tags, etc. with the help of the functions used directly in the page body. Relevant function outputs are displayed in the prologue, i.e. up the code they were set.

The processing technique was created first of all in order to be used in components that are usually displayed in the page body, but at the same time a page header, a navigation chain point, and a control panel button, etc. may be added inside these components. Deferred functions cannot be used in the files of the component template template.php and result_modifier.php (because the results of their execution are cached).

Components may be connected inside a deferred function but in this case CSS and js files must be connected manually.

Note: There is a number of new functions that may work under the conditions of caching (SetViewTarget and EndViewTarget). But such functions are new and are not described in the documentation; hence they are rather considered an exception.

Operation Algorithm of This Processing Technique:

  1. Any outgoing flow from PHP script is buffered.
  2. As soon as one of the following methods is found in the code:
    • CMain::ShowTitle(),
    • CMain::ShowCSS(),
    • CMain::ShowNavChain(),
    • CMain::ShowProperty(),
    • CMain::ShowMeta(),
    • CMain::ShowPanel()

    or other function ensuring the deferral of the execution of any function:

    1. All previously buffered content is memorized in the next element of the A stack memory;
    2. An empty element is added to the A stack that in the future will be filled with a deferred function result;
    3. The name of the deferred function is memorized in the B stack;
    4. The buffer is cleaned and bufferization is enabled again.

    Thus, the A stack contains all the page content divided into parts. In the same stack there are empty elements intended for their further fill up with deferred function results.

    There is also a B stack where the names and parameters of deferred functions are memorized in accordance with their sequential order in the code.
  3. At the foot of the page in the service part of the epilogue the following actions are performed:
    1. All deferred functions from the B stack start being executed one by one;
    2. Deferred function results are entered into a specially designated places in the A stack;
    3. All content from the A stack is “stuck together” (concatenated) and displayed on the screen.

Thus, the processing technique permits fragmenting all the page content by dividing it into parts using special functions that ensure the temporary deferral of execution of other functions (deferred functions). At the foot of the page, all the deferred functions are executed one by one, and their results are introduced into specially designated places inside the fragmented content of the page. After that, all the content is stuck together and sent to the site visitor’s browser.

Attention! When using this processing technique it must be taken into account that no actions can be performed with the results of the functions that ensure the deferral of other functions.

An example of the code in which the deferred function will not execute code in the template as expected:

if (!$APPLICATION->GetTitle())
  echo "Standard page";
else
  echo $APPLICATION->GetTitle();

But this code will work:

$APPLICATION->AddBufferContent('ShowCondTitle');

function ShowCondTitle()
{
  global $APPLICATION;
 if (!$APPLICATION->GetTitle())
    return "Standard page";
 else
    return $APPLICATION->GetTitle();
}

Another example

$page_title = $APPLICATION->GetPageProperty(title); // this function does not return anything
if (strlen($page_title)<=0) $page_title = "Default page header";
echo $page_title;

this code will not work because all the deferred functions are executed at the very foot of the page, in the service part of the epilogue.

Example:

<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetTitle("Old header");
?>
<?
global $APPLICATION;
$strTitle = $APPLICATION->GetTitle();
echo $strTitle." - Page header

"; $APPLICATION->SetTitle('New header'); ?> <?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/footer.php");?>

The page will display "Old header" and the browser – "New header".

Groups of functions involved in this processing technique:


Name of the deferring function Deferred functionAdditional related functions
CMain::ShowTitle CMain::GetTitle CMain::SetTitle
CMain::ShowCSS CMain::GetCSS CMain::SetTemplateCSS
CMain::SetAdditionalCSS
CMain::ShowNavChain CMain::GetNavChain CMain::AddChainItem
CMain::ShowProperty CMain::GetProperty CMain::SetPageProperty
CMain::SetDirProperty
CMain::ShowMeta CMain::GetMeta CMain::SetPageProperty
CMain::SetDirProperty
CMain::ShowPanel CMain::GetPanel CMain::AddPanelButton

The processing technique permits creating deferred functions using the method CMain::AddBufferContent.



Courses developed by Bitrix24