I
I
Insom842021-05-21 15:21:19
1C-Bitrix
Insom84, 2021-05-21 15:21:19

Assign class to div if Bitrix main page is open?

There are 2 div elements with id="one" and with id="two" how to assign classes to them if the main page of the site is open (for example, classes "red" for div with id="one" and class "green" for div with id ="two" respectively)?
I understand the condition looks like this:

<? if ($APPLICATION->GetCurPage(false) === '/'): ?>
.....
<? endif; ?>

Can you please tell me what should be in it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ivashjke, 2021-05-21
@ivashjke

$mainPageClass = "/" === $APPLICATION->GetCurPage(false) ? " red green " : ""
echo '<div id="one" class="someClass' , $mainPageClass, '"></div>';

A
Alexander, 2021-05-21
Madzhugin @Suntechnic

Terrible way - never do that.
With this approach, you turn the content management system into a page management system.
What difference does it make what URL your page is opened at? The page should open correctly regardless of the url.
If your divs are inside this page, just give them classes.
If your divs are inside some component that is called from the page script and from other places - just add a parameter to the component in which you pass an additional class.
If your divs lie outside of the page script (I suspect that this is most likely your case), use deferred functions:

<div id="one" class="someClass <?=$APPLICATION->ShowProperty('color_class')?>">

And in the page:
<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$APPLICATION->SetPageProperty('color_class', 'green');
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question