[Solved] Select files that are direct children of content, not in sub-folders

#1

I installed and customised a theme that creates a single page out of multiple .md files. When creating a page, this theme takes into account any file in content, including those in sub-folders. I would like to create pages only from direct children, especially for the front page (the one at localhost:1313).

/content/
    child1
    child2
    section1/
        grandchild1

I want to build a page using only child1 and child2, excluding grandchild1

I tried to set up an index.md file at the root of content, but this prevents me from accessing other pages.

I’m trying to find, if possible, a good where function usage to create this condition. Keep in mind that I’m a newbie and I’m not a Go programmer.

In terms of code, the following line includes all pages :

{{ range $index, $element :=  .Data.Pages}}

I would like to do something like :

{{ range $index, $element :=  where .InSection(.Data.Pages)}}

Unfortunately, I can’t get this to work.

Here is the full code :

<section id="services">

<!-- Page Content -->
{{ range $index, $element :=  .Data.Pages}}
    {{ if modBool $index 2 }}
    <div class="content-section-a">
    {{ else }}
    <div class="content-section-b">
    {{ end }}

        <div class="container">

            <div class="row">
                <div>
                    <hr class="section-heading-spacer">
                    <div class="clearfix"></div>
                    <h2 class="section-heading">{{ .Title }}</h2>
                    {{ .Content }}
                </div>
            </div>

        </div>
        <!-- /.container -->

    </div>
{{ end }}

</section>

Hugo env :

Hugo Static Site Generator v0.34 linux/amd64 BuildDate: 2018-01-22T12:06:40Z
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.9.2"
#2

It seems to be that page bundles and the new .Resources template variable are what you are looking for. I’m still trying to move my content into that sort of structure myself.

#3

You should ask the theme author for assistance.

#4

Since I’ve customized the theme, the problem doesn’t really come from the theme. I’m trying to create a condition like where .InSection .Data.Pages but I don’t know how to code it properly.

Basically, I have the following line that takes every file into account :

{{ range $index, $element :=  .Data.Pages}}

I want to take only the file that are direct children of content, not the files in subfolders.

#5

If you want assistance with a template, please read and follow the guidelines at Requesting Help, then someone will be able to help. :slight_smile:

#6

I solved it after multiple trials and errors :

{{ range $index, $element :=  where .Data.Pages "Section" .Section}}
1 Like