How to slice within a range

Hi,

I’m struggling to find the correct syntax for what I want to do.

Like a “Read more articles” section, I want to exclude the active current page from a list of pages.

I have a “translationKey” in my .md files (pages), so I was planning to use that to do the comparison, which may not be the best way, but works for me (I’m all ears of you have a better way to do that)

{{ $section := site.GetPage "references" }}
<ul class="references">
    {{ range $section.Pages | first 3 | slice [need help here] }}
        <li class="reference">
            <div class="reference__img">
                <img src="{{ .Params.img }}" alt=""/>
            </div>
            <p class="reference__description">{{ .Description | safeHTML }}</p>
        </li>
    {{ end }}
</ul>

Despite reading the docs I can’t get the right syntax.

Thank you for your help.

Maybe something like:

{{/* Get pages in current section. */}}
{{ $p := where .Site.RegularPages "Section" .Section }}
{{/* Now exclude the current page based on Title. */}}
{{ $p = where $p "Title" "ne" .Title }}
{{ range first 3 $p }}
  {{ .Title }}<br>
{{ end }}

I’ve figured a way to do it with an if statement, but I’m pretty sure it exists a more elegant way !

{{ $section := site.GetPage "references" }}
<ul class="references">
{{ $key := .Params.translationKey }}
    {{ range $section.Pages }}
        {{ if not (eq .Params.translationKey $key) }}
        <li class="reference">
            <div class="reference__img">
                <img src="{{ .Params.img }}" alt=""/>
            </div>
            <p class="reference__description">{{ .Description | safeHTML }}</p>
        </li>
        {{ end }}
    {{ end }}
</ul>

We replied at the same time :grinning: I like your solution more, thanks !

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