# Range through sub-section

**URL:** <https://discourse.gohugo.io/t/range-through-sub-section/33453>\
**Category:** support\
**Tags:** tailwind\
**Created:** [June 19, 2021, 4:18pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453 "2021-06-19T16:18:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bootyocean18](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bootyocean18/32/20519_2.png) [@bootyocean18](https://discourse.gohugo.io/u/bootyocean18)\
**Post date:** [June 19, 2021, 4:18pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/1 "2021-06-19T16:18:01Z")

</div>

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:

```auto
/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:

```auto
{{ 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

- [The page that works as intended](https://luanre.netlify.app/answers/)
- [The broken page](https://luanre.netlify.app/answers/covid-19/)
- [The Repo](https://gitlab.com/rizdy/luanre) (Note: You can also see my actual directory structure here)

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

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 19, 2021, 4:32pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/2 "2021-06-19T16:32:39Z")

</div>

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

```auto
{{ $subject := site.GetPage "documentation/subject" }}
{{ $subject.Pages }}
{{ $subject.RegularPages }}
{{ $subject.RegularPagesRecursive }}

```

---

<div class="post-metadata">

**Author:** ![bootyocean18](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bootyocean18/32/20519_2.png) [@bootyocean18](https://discourse.gohugo.io/u/bootyocean18)\
**Post date:** [June 19, 2021, 4:36pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/3 "2021-06-19T16:36:36Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 19, 2021, 4:37pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/4 "2021-06-19T16:37:10Z")

</div>

> [@bootyocean18](#):
>
> Is there a way I can set this up so that it grabs `current-directory` ?

What is current-directory?

---

<div class="post-metadata">

**Author:** ![bootyocean18](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bootyocean18/32/20519_2.png) [@bootyocean18](https://discourse.gohugo.io/u/bootyocean18)\
**Post date:** [June 19, 2021, 4:41pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/5 "2021-06-19T16:41:23Z")

</div>

So my actual file structure is

```auto
/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.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 19, 2021, 4:46pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/6 "2021-06-19T16:46:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bootyocean18](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bootyocean18/32/20519_2.png) [@bootyocean18](https://discourse.gohugo.io/u/bootyocean18)\
**Post date:** [June 19, 2021, 5:10pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/7 "2021-06-19T17:10:37Z")

</div>

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

```auto
{{ 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 }}

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [June 21, 2021, 5:10pm UTC](https://discourse.gohugo.io/t/range-through-sub-section/33453/8 "2021-06-21T17:10:59Z")

</div>

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