3. Highlighting in the navigation
The current page should appear in bold in the main navigation.
To do this, we check in the template that creates the navigation items whether the item in question is the current page.
First, we remember the number of the requested page. To do this, add the following at the very beginning of the template header:
<?php
$pageId = $cms['record_id'] ?? 0;
?>
The record number record_id is in the PHP array $cms. We store it in the variable $pageId because, in the template navigation, $cms contains the data of the respective navigation item with every pass.
In the template navigation, we then check within the loop whether the navigation item is the current page:
<ul>
(CMS:RESULT_START)
<li style="padding-left:(CMS:NUMBER depth mul=20)px;"><a href="(CMS:URL)">
<?php
if ($cms['record_id'] == $pageId):
?>
<strong>(CMS:TEXT title)</strong>
<?php
else:
?>
(CMS:TEXT title)
<?php
endif;
?>
</a></li>
(CMS:RESULT_END)
</ul>
Between (CMS:RESULT_START) and (CMS:RESULT_END), $cms contains the data of the next record found with every pass.
On the website, the navigation item of the current page now appears in bold.
This works as long as you click through the navigation items. But as soon as you click a project on one of the “… housing” pages, no menu item is bold any more – of course, because none of the menu items is a project itself.
So we make two changes:
A) The page the project belongs to (“Private housing” or “Urban housing”) should appear in bold in the navigation.
B) For every project, the main item “Projects” should also appear in bold.
A) Highlight the navigation subitem
Open the template page_project_details and add the following at the very beginning of the code:
(CMS:SELECT save_id
relation=rel1_pages
)
After saving, click the template name save_id to create the template:
Name: save_id
Code:
(CMS:RESULT_START)
(CMS:SAVEDATA projectPage)
(CMS:RESULT_END)
What these changes do: The template used to call up a project first determines the linked page and stores all of its data ($cms) in the PHP array $projectPage.
Now we extend the template navigation:
<ul>
(CMS:RESULT_START)
<li style="padding-left:(CMS:NUMBER depth mul=20)px;"><a href="(CMS:URL)">
<?php
if ($cms['record_id'] == $pageId || $cms['record_id'] == ($projectPage['record_id'] ?? 0)):
?>
<strong>(CMS:TEXT title)</strong>
<?php
else:
?>
(CMS:TEXT title)
<?php
endif;
?>
</a></li>
(CMS:RESULT_END)
</ul>
If the navigation item is the requested page itself OR (||) the page of the requested project, it is printed in bold.
On all pages that are not a project, $projectPage does not exist. ?? 0 then uses the value 0 instead and thus avoids a PHP warning.
B) Highlight the main navigation item
At the beginning of the template header, replace the PHP code with:
(CMS:SAVEDATA page)
This way, we no longer store just the record number of the current page, but all of its data – in $page.
In the template navigation, we change the check: Instead of comparing with $pageId, we now compare with $page['record_id']:
<?php
if ($cms['record_id'] == $page['record_id'] ...
We also add another condition:
<?php
if (
$cms['record_id'] == $page['record_id']
|| $cms['record_id'] == ($projectPage['record_id'] ?? 0)
|| $page['area_id'] == 3 && $cms['title'] == 'Projects'
) :
?>
Meaning of the line $page['area_id'] == 3 && $cms['title'] == 'Projects':
“If the requested page comes from the content area with the number 3 (the area ‘Projects’) AND the navigation item in the loop is called ‘Projects’ …”
You can find the area number in the right-hand column of the module “Structure”. In our example, the first template area was given the number 1, the content area “Pages” the number 2 and “Projects” the number 3. If your numbers differ, use the number of your area “Projects”.
So that the main item also appears in bold when we call up a subpage (not a project), we add one more line:
<?php
if (
$cms['record_id'] == $page['record_id']
|| $cms['record_id'] == ($projectPage['record_id'] ?? 0)
|| $page['area_id'] == 3 && $cms['title'] == 'Projects'
|| $page['depth'] == 1 && $cms['title'] == 'Projects'
) :
?>
“If the requested page is a subpage on the first level (depth = 1) AND the navigation item is our page ‘Projects’ …”
This is sufficient here because so far only the page “Projects” has subpages.