33

Just wondering how people approach setting a class of current in their navigation. Is there anything in Craft or Twig where you can set a conditional to check if a page is the current page. Or do you just need to set classes/ids on the body tag based on the segment?

Any ideas appreciated, Gareth

11 Answers 11

32

Tip: To keep it short and reusable I would recommend that you wrap your preferred "check-method" in a Twig macro, I like Anna's version the most:

_macros.html:

{% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}

layout.html:

{% import "_macros.html" as macros %}

<a href="{{ url('news') }}" class="{{ macros.is_active('news') }}">News</a>
<a href="{{ url('blog') }}" class="{{ macros.is_active('blog') }}">Blog</a>
  • How would you test for pluralisation? So ('projects' || 'project') would both return an 'active' class? – Dan Christian Jan 15 '17 at 12:17
29

Assuming you use a structure called pages to create your navigation and the entry variable represents the currently viewed entry, you can output your navigation and active state as follows:

<ul>
{% nav page in craft.entries.section('pages') %}
    <li class="{{ page.id == entry.id ? 'active' }}">
        {{ page.link }}
        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}
    </li>
{% endnav %}
</ul>

See the nav tag for more details.

If, instead of a structure, you are using a channel called pages for the navigation then the nav tag method above will still work but the following code is leaner and simpler to understand:

<ul>
{% for page in craft.entries.section('pages') %}
    <li class="{{ page.id == entry.id ? 'active' }}">
        {{ page.link }}
    </li>
{% endfor %}
</ul>
  • Thanks Ben, I hadn't even looked into the nav tag, this only works for a structured section though, right? Still very useful, thank you. – Gareth Jun 28 '14 at 15:03
  • it should work for a channel too, i updated my answer above with code for a channel though. – Ben Croker Jun 30 '14 at 9:01
  • If using this with a dedicated menu section, this shouldn't work? Because nav items in this case are pages too and the ids of a nav item and a page corresponding to it are always different. – certainlyakey Aug 22 '15 at 7:51
14

craft.request is useful for setting an "active" class based on URL segments.

If your URL was: domain.com/sale/products/shirts

You could write conditionals like the following to target each segment:

{% if craft.request.firstSegment == "sale" %}class="active"{% endif %}
{% if craft.request.segment(2) == "products" %}class="active"{% endif %}
{% if craft.request.lastSegment == "shirts" %}class="active"{% endif %}
  • 1
    Thanks Anna, I was thinking down this route, great minds :) thank you for your answer - have you seen Victor's suggestion using a macro with your code, looks like a nice we to keep things DRY. – Gareth Jun 28 '14 at 18:18
12

Barrel Strength also have a URL segment based plugin called Sprout Active, with docs/examples here.

  • Sprout Active is great! I think the best solution for this. – Johannes Lamers Aug 8 '14 at 22:25
10

In your template you can set a var in twig:

{% set active = 'blog' %}

And then in your navigation you can run an if statement to see what the var is

<nav>
    <a {% if active = 'homepage' %}class="active"{% endif %}> Home </a>
    <a {% if active = 'blog' %} class="active"{% endif %}> Blog </a>
</nav>
  • Thank you Rob, this looks as if it works great until you re-use a template. Then I guess you would need to use a craft.request to check the segment variable? – Gareth Jun 28 '14 at 15:07
  • Yeah you are probably right. For simple sites using Craft Personal you can get away with my above answer. However if you're doing something much larger, you might want to check out @Anna_MediaGirl's answer. – Rob Erskine Jun 30 '14 at 14:36
9

Here's yet another way you can do it (in some circumstances) and it's nice and DRY:

Craft 2

{% if craft.request.getPath() ==  entry.uri %}

Craft 3

{% if craft.app.request.pathInfo ==  entry.uri %}
  • That doesn't seem tp work on the index page because entry.uri on the homepage is __home__ – KSPR Dec 5 '14 at 16:17
7

Another way to achieve this:

    {% for entry in entries %}
      {% set active = craft.request.path ~ '/' matches "|^#{entry.uri}/|" %}
      <a href="{{ entry.url }}" class="{{ active ? 'is-active' : '' }}">{{ entry.title }}</a>
    {% endfor %}

This accounts for even when a navigation item might not be on the top level, but you want every URI that starts with that to trigger the active class.

Note: the pipes around the regex are just the encloser. They can be anything, but since we're dealing with URLs, it's easier not to use the standard /.

6

I add a variable in my "_entry" template.

Here's a basic example:

{% extends "_layout" %}

{% set bodyClass = 'myPageType' %}

{% block content %}
<!-- entry HTML here -->
{% endblock %}

Then in my _layout template, it begins like this:

<!doctype html>
<html class="{% if bodyClass is defined %}{{ bodyClass }}{% endif %}">
<!-- the rest of the template follows -->

Does something like that work for you?

  • Hey, John there are a few different options now, this page has become a great resource, thank you for your answer. – Gareth Jun 28 '14 at 18:20
6

Here's one more for this nice collection!

This is what I use for a structure "pages" where I needed some way to not only add a class active to the current entry, but also to all of the entry's ancestors. In an Example URL http://example.com/beers/wheat-beers/ both links beers and wheat-beers should be given the activeclass.

The macro takes the structure entry's uri as one parameter. The other parameter is needed to specify the part of the current URL, the entry's URI gets compared to (→ craft.request.getSegment()).

Give this parameter a value based on the entry's level within its structure.

.

_macros.html:

{% macro activeLinkClass(linkUri, requestSegmentNumber) %}

    {# Request the relevant path segment in the URL #}
    {% set nthRequestSegment = craft.request.segment(requestSegmentNumber) %}

    {# Split the link's URI into a list of path segments #}
    {% set linkSegments = linkUri|split('/') %}

    {# Get the path segment relevant for our comparison #}
    {% set nthLinkSegment = linkSegments[requestSegmentNumber - 1] %}

    {# Return `active` if the nthLinkSegment equals the nthRequestSegment #}
    {% if nthLinkSegment == nthRequestSegment %}active{% endif %}

{% endmacro %}

.

_navigation.html:

{% import '_macros.html' as macros %}

{# Get entries from pages structure (exclude home page)  #}
{% set pages = craft.entries.section('pages').id('not 1') %}

{# Loop through entries and list the links #}
{% nav page in pages %}

    {# Add a class `active` if needed, using our macro #}
    <a href="{{ page.url }}" class="nav-link{{ macros.activeLinkClass(page.uri, page.level) }}">
        {{ page.title }}
    </a>

    {% ifchildren %}{% children %}{% endifchildren %}

{% endnav %}
4

I think comparing the entry id's is the best way to go for the current page. But for determining if a certain page is a parent of the current page, I combined answers from @tim-kelty and @carlcs to come up with this:

{% for entry in entries %}
  {% set isInSection = craft.request.firstSegment == entry.uri|split('/')[0] %}
  <a href="{{ entry.url }}" class="{{ isInSection ? 'active' : '' }}">{{ entry.title }}</a>
{% endfor %}

EDIT: I realized my above code does something slightly different than I originally thought: it will add the 'active' class to any items that are in the current' page's section... which includes siblings. If that's what you want, then it's great. But if you only want to indicate the actual top-level section parent page, then you should do this instead:

{% for entry in entries %}
  {% set isTopParent = craft.request.firstSegment == entry.uri %}
  <a href="{{ entry.url }}" class="{{ isTopParent ? 'active' : '' }}">{{ entry.title }}</a>
{% endfor %}

Last but not least, if you want to hilite all pages that are in the parent "path" of the current page (which is what I most often need in my menus), you can do this:

{# use different var name in the loop because we assume `entry` is already set to the currently-viewed page #}
{% for page in entries %}
  {% set isInPath = entry.uri matches '{^' ~ page.uri ~ '}' %}
  <a href="{{ page.url }}" class="{{ isInPath ? 'active' : '' }}">{{ page.title }}</a>
{% endfor %}

I've posted a full example that encompasses a lot of "denote the active page or its parents" logic here: http://craftcookbook.net/recipes/388

2

I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want dynamic information from the section ID number.

If you use multilingual sites, this is super practical because it will get the correct language page title, URL and check if it's currently active.

<li>                    
    {% set homepage = craft.entries.id(13).one() %}
    <a href="{{ homepage.getUrl }}" {% if craft.app.request.absoluteUrl == homepage.url %} class="active"{% endif %}>{{ homepage.title }}</a>
</li>
<li>                    
    {% set about = craft.entries.id(23).one() %}
    <a href="{{ about.getUrl }}" {% if craft.app.request.absoluteUrl == about.url %} class="active"{% endif %}>{{ about.title }}</a>
</li>

As you can see in the example above I get 2 pages for my navigation, home and about pages.

Hopefully this helps out someone in the near future.

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.