Views: 9597
Last Modified: 02.10.2018

Simple examples of template application depending on various conditions

If the phone section property is equal to Y

$APPLICATION->GetDirProperty("phone")=="Y"

If the current section is equal to /en/catalog/phone/

$APPLICATION->GetCurDir()=="/en/catalog/phone/"

If a current user is the administrator

$USER->IsAdmin()

If the template must be linked to a dynamic page (in the example, to a detailed page (card) of the goods)

preg_match("#/catalog/\?SECTION_ID=\d+&ELEMENT_ID=\d+#i",$_SERVER['REQUEST_URI']);

Connection of Favicon.ico for Various Templates

In order to have different symbols in different site templates a call of a symbol from the template must be added to header.php of the template:

<link rel="icon" type="image/x-icon"
href="<?=SITE_TEMPLATE_PATH;?>/images/favicon.ico" />

Separate Template for the Home Page of the Site

In addition to the main template for all site pages, a required template must be connected through the condition For folder or file indicating /index.php and taking into account the application sorting index of the template.


Change of the Site Template Depending on the Time Parameter

Task: How to implement a site template change depending on the time parameter (two templates in total, but they must be changed every hour)?

For this, the current template must be changed according to the condition PHP expression.

First Option

On odd hours: ((date("H")+6)%2) == 1
On even hours: ((date("H")+6)%2) == 0

where +6 indicates the time zone.

Second Option

date("H")%2
or
!date("H")%2



Application of the Template Using Both Conditions Simultaneously

Task: How to set a template according to 2 conditions simultaneously (for a group of users and for both folder and file)?

For this, the current template must be changed according to the condition PHP expression:

in_array($groupID, $USER->GetUserGroupArray()) || strpos($APPLICATION->GetCurDir(), "/dir/")!==false || $APPLICATION->GetCurPage()=="/dir/file.php"

Template Application Only to the Files with a Specific Extension

Task: Which PHP expression should be used so that the template applies to all pages ended in .php but not .html?

Solution: change of the template according to the condition PHP expression:

substr($APPLICATION->GetCurPage(true), -4) == ".php" 

Similarly for the files with the html extension:

substr($APPLICATION->GetCurPage(true), -5) == ".html"

Change of Site Header Design for Different Sections

Task: The site is divided into several sections. The idea is that each section must have its own header in the design. There are no other changes in the design.

The component Insert include area is connected to the template:

<div id="header">
<?$APPLICATION->IncludeComponent("bitrix:main.include", ".default", array(
   "AREA_FILE_SHOW" => "sect",
   "AREA_FILE_SUFFIX" => "headerinc",
   "AREA_FILE_RECURSIVE" => "Y",
   "EDIT_TEMPLATE" => "sect_headerinc.php"
   ),
   false
);?>
</div> 

The header code of each section will be stored in the file sect_headerinc.php. The parameter "AREA_FILE_RECURSIVE" => "Y" means that the same header will appear in all subsections of this section, unless the parental sect_headerinc.php is specifically shut off in any underlying sections.

Specific of working with AJAX

Using the ajax mode has its specifics. For the navigation string to have its name at the page opened via ajax, the template must have the element with id="navigation". The div is optional: it can be span, h1, p and so on.

Similarly, the header must have the id="pagetitle" element available.





Courses developed by Bitrix24