Jump to content


(Advanced) Building dynamic blocks based on the page being viewed

For more advanced sites built with Pages, you may want to change the output of a custom HTML or PHP block depending on which page the user is viewing. For example, if you have a custom menu, you may want to highlight the active item.

We can implement this in Pages by checking the underlying page URL parameters. Although you access a page with a friendly URL (FURL) like http://<yourcommunity>/section/page, behind the scenes this is mapped to a raw URL, such as http://<yourcommunity>/index.php?app=cms&module=pages&controller=page&path=/section/page. Notice the path parameter allows us to identify which page we're accessing. When we access the \IPS\Request::i() object, we can compare against this parameter, like so:

{{if strpos( \IPS\Request::i()->path, 'section/page' ) !== FALSE}}
	<!-- We know the user is on /section/page -->
{{elseif strpos( \IPS\Request::i()->path, 'othersection/otherpage' ) !== FALSE}}
	<!-- We know the user is on /othersection/otherpage -->

Note that for reliability, we're using PHP's strpos function to check for the presence of the page URL in the path parameter, rather than a simple comparison.


Let's assume we've created a Manual HTML block, we're adding HTML to show a menu, and we want to highlight the correct item based on the page. Here's what our block contents might look like:

<ul class='ipsList_inline cMyMenu'>
  <li {{if strpos( \IPS\Request::i()->path, 'help/home' ) !== FALSE}}class='active'{{endif}}>
    <a href='/help/home'>Home</a>
  <li {{if strpos( \IPS\Request::i()->path, 'help/faq' ) !== FALSE}}class='active'{{endif}}>
    <a href='/help/faq'>FAQ</a>
  <li {{if strpos( \IPS\Request::i()->path, 'help/tutorials' ) !== FALSE}}class='active'{{endif}}>
    <a href='/help/tutorials'>Tutorials</a>

If we had many items to show, it would get tedious to list them all like this. We could instead do it as a loop:

// Using a PHP variable to store an array of pages => page names that we'll loop over
{{$myPages = array('help/home' => "Home", 'help/faq' => "FAQ", 'help/tutorials' => "Tutorials", 'help/qna/listing' => "Questions", 'help/qna/recent' => "Recent Questions", 'help/contact' => "Contact Us");}}

<ul class='ipsList_inline cMyMenu'>
	{{foreach $myPages as $url => $title}}
  		<li {{if strpos( \IPS\Request::i()->path, $url ) !== FALSE}}class='active'{{endif}}>
			<a href='{$url}'>{$title}</a>

Now to add new items to our custom menu, we just have to add them to the array.

  Report Guide

  • Create New...

Important Information

We use technologies, such as cookies, to customise content and advertising, to provide social media features and to analyse traffic to the site. We also share information about your use of our site with our trusted social media, advertising and analytics partners. See more about cookies and our Privacy Policy