How to know when I'm ranging through the current page?

hi there,

I’m using in the side bar of my section “foobar”, a list of pages to point to others in the same section.

{{ range (where .Site.RegularPages "Section" "=" "foobar") }}
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
{{ end }}

But I’ve been searching around how to know when the .RelPermalink is actually the current page so I can mark it is-active and remove the link. Any tips please?

If I understand this correctly, you are trying to check if the page in the loop is the current page that is displayed. If that is what you need, it is pretty straight-forward to implement.
You need to understand that the context (the dot operator) is different within and outside the loop.
When you have not entered in a loop or a conditional, the . represents the current page.
So, at the top of your layout file, just declare a variable that would represent the current page at all times.
{{ $currentPage := . }}
Within your loop, you can check if the current context matches the current page. Add this within your loop:

            {{ if (eq . $currentPage) }}
            your logic for **is-active**
            {{ end }}

In case of your code, it probably could be something like:

{{ range (where .Site.RegularPages "Section" "=" "foobar") }}
<li><a href="{{.RelPermalink}}" class="{{ if (eq . $currentPage) }}active{{ end }}">{{.Title}}</a></li>
{{ end }}
3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.