Hugo

Range where with parent page / run only if range not empty

Hi,

I was looking at the Docsy theme file layouts/partials/section-index.html and I have a couple of questions:

  1. Can I rewrite this using range where?
{{ $pages := (where .Site.Pages "Section" .Section).ByWeight }}
{{ $parent := .Page }}
{{ range $pages }}
    {{ if eq .Parent $parent }}
        <li><a href="{{ .RelPermalink }}">{{- .Title -}}</a></li>
    {{ end }}
{{ end }}
  1. Can I rewrite this in such a way the <hr> is only run if the range $pages + if eq is not empty?
{{ $pages := (where .Site.Pages "Section" .Section).ByWeight }}
{{ $parent := .Page }}
<hr class="panel-line">
{{ range $pages }}
    {{ if eq .Parent $parent }}
        // Show only one <hr> if this happens
    {{ end }}
{{ end }}

I've tried many syntax variants, search the forums but got nowhere.

Thanks!

Yes, you can do one long range where ... statement.

Sure. Here’s one way, there are others:

{{ $hr := 0 }}
{{ range $pages }}
    {{ if eq .Parent $parent }}
        // Show only one <hr> if this happens
        {{ if lt $hr 1 }}
        <hr>
        {{ $hr = add $hr 1 }}
        {{ end }}
    {{ end }}
{{ end }}

But how do I do it? I’ve tried using range where $pages "Parent" $parent but it does not provide the same results.

{{ $parent := .Page }}
{{ range (where .Site.Pages "Section" .Section).ByWeight }}
  {{ if eq .Parent $parent }}
...

It looks like you are trying to print out the child pages of the current page. Why not instead just use .RegularPages?

{{ range .RegularPages }}
1 Like