2

I am trying to set a conditional on a navigation element using segmentation, and this works fine except when I hit the pagination stuff.

The pagination is in increments of 50 so I tried the following but it only works if the page is P50.

<ul>
<li><a {if segment_2 == "" OR segment_2 == "article" AND segment_2 >="P50"}class="active"{/if} href="{site_url}news/">Clean Energy News</a></li>
<li><a {if segment_2 == "releases"}class="active"{/if} href="{site_url}news/releases">News Releases</a></li>
<li><a {if segment_2 == "multimedia"}class="active"{/if}{if segment_2 == "photos"}class="active"{/if} href="{site_url}news/multimedia">Multimedia</a></li>
<li><a {if segment_2 == "archive"}class="active"{/if}href="{site_url}news/archive">News Archive</a></li>
</ul>

Is there a conditional I can put on segment_2 that will work? Note that I have other nav elements that I don't want it to interfere with. Thanks!

3

Take a look at the MD Detect Page Type or Switchee addons.

|improve this answer|||||
1

For navigation, whether I am using an embed method or the template partials with Stash approach, I find it easier to pass a parameter specifying which section I want active rather than trying to detect it manually.

In my experience there is always an edge case requiring a more manual / granular approach that will defeat any automatic setup down the road.

Embeds

In main template

{embed="embeds/.header" active_nav="articles"}

In embedded template

<ul>
    <li><a{if embed:active_nav == "articles"} class="active"{/if} href="{site_url}articles/">Articles</a></li>
    <li><a{if embed:active_nav == "news"} class="active"{/if} href="{site_url}news/">News</a></li>
    ...
</ul>

template partials

In partial template

{embed="layouts/.main"}
{exp:stash:set_value name="active_nav" value="articles"}

In layout (.main) template

<ul>
    <li><a{if "{exp:stash:get name='active_nav'}" == "articles"} class="active"{/if} href="{site_url}articles/">Articles</a></li>
    <li><a{if "{exp:stash:get name='active_nav'}" == "news"} class="active"{/if} href="{site_url}news/">News</a></li>
    ...
</ul>

Variant with body class only

In partial template

{embed="layouts/.main"}
{exp:stash:set_value name="body_class" value="body_articles"}

In layout template

<body{if {exp:stash:not_empty name="body_class"}} class="{exp:stash:get name='body_class'}"{/if}>

<ul>
    <li class="articles"><a href="{site_url}articles/">Articles</a></li>
    <li class="news"><a href="{site_url}news/">News</a></li>
...
</ul>

in your CSS

.body_articles .articles > a
{
    do something
} 

From the two add-ons mentioned by Anna, I would go down the switchee route

{exp:switchee variable="{segment_2}" parse="inward"}

    {case value="#^P(\\d+)$#|''"}
        code if pagination page
    {/case}

    {case default="Yes"}
        code if not a pagination page
    {/case}

{/exp:switchee}

The advantage of switchee beeing that, if you have channel entries loops or anything heavy process-wise, it only get executed should the tested condition apply. Better for performance.

|improve this answer|||||
  • No problem. Hope it will help fmz too. Using such a method for navigation instead of a more automated solution really saved my bacon on some occasions – Jérôme Coupé Jan 14 '13 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.