Back to Home

Alternative implementation of templates in 1C-Bitrix

bitrix · 1s-bitrix · pattern · alternative

Alternative implementation of templates in 1C-Bitrix

    First, let me remind you how templates are implemented in a standard form for 1C-Bitrix. All templates are stored in the / bitrix / templates / folder. The template folder contains the required header.php and footer.php files. In fact, these two files together form a page template, i.e. first header.php is connected, then the content of the page, then footer.php. The scheme is dumb to insanity. First of all, the fact that the template is divided into two parts and you can edit it as a whole only through the administration system. And this is a clinic. The second inconvenience of such a scheme is that, in fact, we cannot use page variables in the header.php part, since at the time the first part of the template was called, the system still did not “know” about these variables. Of course, you can use the proposed option for buffering data, but again, such variables can only be used for output.

    A typical situation. A parameter is set on the page that determines whether to display the page title or not. This cannot be done using standard Bitrix tools, since the title is shown before the page content. The way out of the situation is to use your template system. I developed it already a year and a half ago and successfully use it in all my projects. So, the system is as follows. We buffer the entire content of the page, attach the template, and paste the content into it as a variable. A practical implementation is as follows:

    header.php file: footer.php file: template.php file:
    <‍?php
    if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
    ob_start();
    ?>



    <‍?php
    if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
    $WORK_AREA = ob_get_clean(); // Получаем контент рабочей области страницы
    include_once("template.php");
    ?>



    <‍?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>
    <‍html>
    <‍body>
    <‍?=$WORK_AREA?>
    <‍/body>
    <‍/html>


    As a result, we have a complete website template in the template.php file. We solve two main problems voiced earlier. The only minus is that editing the template through the admin panel is no longer possible. Although this can also be considered a plus.

    Read Next