Range through sub-section

Building out a documentation site that also has blogging and podcasting functionality (which are just copies of each other, tbh).

I’m working on the documentation side of it now and I’m having a bit of a problem ranging through subdirectories.

Content:

/Content
  |- _index.md // home page
  |
  |- blog/
  |  └-- post-one/
  |       |- index.md
  |       └- asset.jpg
  |
  |- documentation/
  |  |- _index.md
  |  |- subject/
  |     |- _index.md
  |     └- specific-article/
  |        |- index.md
  |        └- asset.jpg
  |
  └- podcast/

On the documentation side, I have the intitial level page set up perfectly: Range through the subject sub-directories and pull their index pages for “categories”, and below that range all of the content pages of everything below itself. Easy enough.

Here’s how I achieved that:

{{ if eq .Section "answers" }}
  <h2 class="text-2xl text-gray-700 mt-7 mb-7">Answers by Topic</h2>

  <section class="w-full flex flex-wrap flex-row">
    {{ range .Data.Pages }}
      {{ if .IsSection }}
        <a href="{{ .Permalink }}" class="flex flex-row items-center bg-gray-200 shadow m-2 py-5 px-7 rounded-xl cursor-pointer hover:bg-white transition-all duration-200 hover:shadow-lg">
          <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 mr-2 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
          </svg>
          {{ .Title }}
        </a>
      {{ end }}
    {{ end }}
  </section>

  <h2 class="text-2xl text-gray-700 mt-14">Browse All Answers</h2>

  <section class="text-gray-600 body-font">
    <div class="container px-5 py-8 mx-auto">
      <div class="flex flex-wrap -mx-4 -my-8">
        {{ range .Site.RegularPages }}
          {{ .Render "summary" }}
        {{ end }}
      </div>
    </div>
  </section>

{{ end }}

My problem is when I’m in a subdirectory.

When I’m sitting at my documentation root this is perfect. But when I’m down side a specific /subject directory I want to only list the pages inside that subject.

I don’t knwo if I need to create an entirely seperate template (hope not!), or if I can modify mine up there to include some kind of conditional or extra range?

Links

I hope my question makes sense! Let me know if I need to clarify anything. Any suggestions appreciated.

There are several useful methods on a section that I think gives you what you want, e.g.:

{{ $subject := site.GetPage "documentation/subject" }}
{{ $subject.Pages }}
{{ $subject.RegularPages }}
{{ $subject.RegularPagesRecursive }}
1 Like

Thank you! That seems a lot easier than I was trying to make it lol

Is there a way I can set this up so that it grabs current-directory?

What is current-directory?

So my actual file structure is

/content
    - /answers
        - /subject
            - /article

When I’m at example.com/answers (/content/answers) I want it to range through every /article folder under every subject folder.

But when I’m at example.com/answers/subject (/content/answers/subject) I only want to range that specific /subject/ folder.

You would then use $answers.RegularPagesRecursive and $subject.RegularPages … The logic for when to call which … I don’t understand your structure good enough to say (you could probably call .Parent and see what that is, e.g. if .Parent.IsHome). But assuming /subject is always one level, you could use RegularPagesRecursive for everything.

1 Like

The .IsHome is a decent solution. I ended up getting this to work:

{{ if .Parent.IsHome }}
  <h2>Answers by Topic</h2>

  {{ range .Data.Pages }}
    {{ if .IsSection }}
        <a href="{{ .Permalink }}">{{ .Params.Topic }}</a>
    {{ end }}
  {{ end }}

    <h2>Browse All Answers</h2>
      {{ range .RegularPagesRecursive }}
        {{ .Render "summary" }}
      {{ end }}

{{ else }}

      <h2>Browse {{ .Params.topic }} Answers</h2>
        {{ range .Page.Pages }}
          {{ .Render "summary" }}
        {{ end }}

{{ end }}

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