Help - Search - Members - Calendar
Full Version: Detecting The Current Page For A Dynamic Menu
Movable Type Community Forum > Using Movable Type > Templates and Tags
bwells
Hi, I'm working on my first MT site. Its at http://prototypes.wcctechhelp.com/mt/, and I'm using version 4.21.

For the main navigation/menu at the top, I want MT to automatically add class="current" to the <li>. Problem is, mt:PageBasename returns nothing for the Home and News pages, which are actually index templates, not "pages" in the MT sense. To make it more complex, I want to leave "News" highlighted while a user is browsing a monthly archive, or individual entry page.

Is there such a thing as a tag that tells you the URL of what your currently browsing, regardless of whether its an index template, or archive, or page?

Here's the code I'm using:
CODE
<!-- Need some function here to set $CurrentPage -->

<div id="menu">
<div class="container">
<ul>
   <li <mt:if var="CurrentPage" eq="home">class="current"</mt:If>><a href="<mt:blogURL />" title="">Home</a></li>
   <li <mt:if var="CurrentPage" eq="news">class="current"</mt:if>><a href="<mt:link template="Acacia News Archive">" title="">News</a></li>
  
   <mt:pages sort_by="title" sort_order="ascend">
      <li <mt:If tag="PageBasename" eq="$CurrentPage">class="current"</mt:If>><a href="<mt:pagepermalink>" title=""><mt:pagetitle /></a></li>
   </mt:pages>
</ul>
</div>
</div>


Thanks in advance for any help!
caribou sue
Hi,
Yes, you can do this. You just need to set $currentpage within the pages listing.

Here's an example that lists the pages in the current folder, and gives the current page a class of "current".

CODE
<MTSetVarBlock name="thisfolder"><MTPageFolder><MTFolderLabel></MTPageFolder></MTSetVarBlock>
<MTSetVarBlock name="thispage"><MTPageTitle></MTSetVarBlock>

<ul id="subnav">

<MTIfNonEmpty name="thisfolder">
<MTPages folder="$thisfolder">
<MTIf tag="pagetitle" eq="$thispage">
   <li class="current"><a href="<MTPagePermalink>"><MTPageTitle></a></li>
  <MTElse>
   <li><a href="<MTPagePermalink>"><MTPageTitle></a></li>
</MTIf>
</MTPages>
</MTIfNonEmpty>


QUOTE (bwells @ Jan 6 2009, 12:56 PM) *
Hi, I'm working on my first MT site. Its at http://prototypes.wcctechhelp.com/mt/, and I'm using version 4.21.

For the main navigation/menu at the top, I want MT to automatically add class="current" to the <li>. Problem is, mt:PageBasename returns nothing for the Home and News pages, which are actually index templates, not "pages" in the MT sense. To make it more complex, I want to leave "News" highlighted while a user is browsing a monthly archive, or individual entry page.

Is there such a thing as a tag that tells you the URL of what your currently browsing, regardless of whether its an index template, or archive, or page?

Here's the code I'm using:
CODE
<!-- Need some function here to set $CurrentPage -->

<div id="menu">
<div class="container">
<ul>
   <li <mt:if var="CurrentPage" eq="home">class="current"</mt:If>><a href="<mt:blogURL />" title="">Home</a></li>
   <li <mt:if var="CurrentPage" eq="news">class="current"</mt:if>><a href="<mt:link template="Acacia News Archive">" title="">News</a></li>
  
   <mt:pages sort_by="title" sort_order="ascend">
      <li <mt:If tag="PageBasename" eq="$CurrentPage">class="current"</mt:If>><a href="<mt:pagepermalink>" title=""><mt:pagetitle /></a></li>
   </mt:pages>
</ul>
</div>
</div>


Thanks in advance for any help!

bwells
That code works great for the pages, however it doesn't work for the Home page (actually an index template, not an MT "page"), or for any of the archive pages. I think the problem is that mt:PageTitle does not return any value when it is called from an index template or archive page.

Is there an equivalent of mt:PageTitle that will work for index templates or archive pages?

Thanks!
caribou sue
Nice looking site, by the way!

You're right -- MT is trying to define the current page's title, and of course it can't if you're not on a page.

You can get around that with template tags that tell MT only to find the page title when it's a page archive:

CODE
<!--SUPER ANNOTATED EDITION!-->

<!--If this is a page template...-->
<MTIfArchive archive_type="page">

<!--...set the page title as a variable.-->
<MTSetVarBlock name="thispage"><MTPageTitle></MTSetVarBlock>

<!--End 'if page archive'-->
</MTIfArchive>

<!--Start listing pages-->
<MTPages>

<!--If this is a page template...-->
<MTIfArchive archive_type="page">

<!--... and if that variable matches the title in a list of pages...-->
<MTIf tag="pagetitle" eq="$thispage">

<!--...give it this class.-->
   <li class="current">

<!--If it doesn't match...-->
</MTIf>

<!--...or it's not a page archive...-->
</MTIfArchive>

<!--...plain old li.-->
   <li>

<!--Stop listing pages.-->
</MTPages>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.