ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Highlighting Selected Item in channel:entries Generated Menu

April 22, 2012 3:11pm

Subscribe [2]
  • #1 / Apr 22, 2012 3:11pm

    NeilC

    28 posts

    Hello,

    I’m trying to implement a menu using the channel:entries tag. The idea is that I have a channel called “seasons” where each entry is a description of a season (as a theatre with season 2011, season 2012, season 2013, etc.), and I want each season to have a link in the menu which shows on the page no matter what season you are currently looking at, thus giving an easy way to navigate between seasons. Here’s the code I wrote for this:

    {exp:channel:entries channel="seasons" sort="asc" limit="3" dynamic="no"}
    <li {if embed:selection == "{title}"} class="selected" {/if}>
    <a href="http://{url_title_path=seasons}">{title}</a></li>
    {/exp:channel:entries}

    Clicking on the link takes the user to the entry for that season using the seasons template. This menu is a template called “interface/menu” and is embedded in the seasons template using this code:

    {embed="interface/menu" selection="{title}"}

    It all works fine except for the selection part. I want the currently viewed season to be highlighted in the menu, and I tried to do this by setting the selection to {title} and then testing for it in the menu code. I can see several reasons why my approach isn’t working. What I can’t figure out is how to make it work the way i want.

    Has anyone else managed to do this, or does anyone have an idea on how I can do this.?

  • #2 / Apr 22, 2012 5:32pm

    glenndavisgroup

    436 posts

    Hi NeilC,

    Try the following:

    {exp:channel:entries channel="seasons" sort="asc" limit="3" dynamic="no"}
    <li {if "{embed:selection}" == "{title}"} class="selected" {/if}>
    <a href="http://{url_title_path=seasons}">{title}</a></li>
    {/exp:channel:entries}

    Mike

  • #3 / Apr 23, 2012 12:10am

    NeilC

    28 posts

    Thanks. I gave that a try and made some progress. Now all of the menu items are highlighted. So I tried using {title} outside of the channel:entry tag so that it would get a value specific to the dynamic entry being displayed on the page, figuring I could set a variable there and then use it inside the channel:entry tag. That failed and was treated as the literal text, “{title}” because those tags are resolved before the template is embedded. Maybe I need to use a snippet instead of an embedded template.

  • #4 / Apr 23, 2012 7:13am

    John St-Amand

    865 posts

    I do channel entries as the menu all the time - normally one of the easiest conditionals to run to determine which one to highlight has to do with segments.  For example, if the URL title for the entry displayed is the last segment in the URL.  Is the season (2012, etc) visible in the URL when the season is selected?  You can do this sort of thing with embeds or snippets (depending on how self-contained it needs to be) - it principally comes down to selecting the best conditional to run for the circumstances to apply your selected class.

    So something simple like this:

    {exp:channel:entries channel="seasons" sort="asc" limit="3" dynamic="no"}
    <li{if last_segment == url_title} class="selected"{/if}><a href="http://{url_title_path=seasons}">{title}</a></li>
    {/exp:channel:entries}

    And so on the subsequent page, visible at seasons/2012, for example, 2012 would be selected, but others would not be, based on the segment matching conditional.

    Hope that helps.

  • #5 / Apr 23, 2012 9:14am

    NeilC

    28 posts

    Thanks, that did the trick. I had tried using segments, but tried testing it against {title}. It hadn’t worked because a title would be “2012 Season” where the last segment was “2012-season”. using {url_title} instead worked. I didn’t know about that variable. Hooray!

  • #6 / Apr 23, 2012 9:15am

    John St-Amand

    865 posts

    Glad to hear it. Post again if you run into anything the community can help with (or of course hit us up on Twitter with #eecms).

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases