Grouping pages by parameter when parameter is not set

Is there a way to not have .Pages.GroupByParam error out if param doesn’t exist in the front matter of some pages? Instead use a default value?

I think a workaround here is to use cascade in sections frontmatter to set the default for leaf pages.
But for some reason .Pages.GroupByParam "my_param" still errors out despite having added “my_param” to _index.md

[cascade]
  my_param= "uncategorized"

I cannot reproduce the problem. Of 8 pages, only 3 have the parameter “foo” in front matter.

{{ len site.Pages }} --> 8

{{ range site.Pages.GroupByParam "foo" }}
  {{ len .Pages }} --> 3
{{ end }}

No errors.

Please share your code, or better yet, a link to your project repository.

I think the OP wants to see an error if the parameter is missing

Seems like there is an error…

A default is not possible. I assume OP would like to have something like this:

{{ range site.Pages.GroupByParam "foo" }}
{{ if (eq . nil) getPage "bar" }}
// do something

This is not possible because a groupBy internally gets rid of the things it can’t group by. That is the same in other programming languages and most probably a Golang thing…

I would have thought of the problem more of some kind of sortBy. Because then the pages that do not have this frontmatter might get sorted first or last in the sorted page collection. But without a real sample to wrap my head around, I can’t say much about this.

I would try to replace groupBy with a sortBy solution to see what goes on with the resulting array.

Edit: on a second thought, sortBy probably throws errors if the frontmatter is missing.

Edit: on a third thought, sortBy in connection with a cascaded frontmatter should do the trick. Now I am pretty sure with a sample repo OP can be helped.

Unfortunately the repo is not public but I’ll try to share some of the code here.
I have the content structured like:

  • content

    • other

    • docs

      • doc-a

        • index.md
      • doc-a

        • index.md
      • _index.md

doc-a\index.md has a parameter in its front matter: group= "special_group" while doc-b\index.md doesn’t have that.
Now in my layout folder I have list view that applies to docs :

{{ define "main" }}
<div class="single-content-area guide-page">

  <div class="main-content {{ if not .Params.page_options.toc }}toc-no{{ end }}">
        <!-- Groups content according to the $groupBy field in front matter -->
        {{ range .Pages.GroupByParam  "group" }}
        <h3>{{ .Key }}</h3>
        <ul>
            {{ range .Pages.ByWeight }}
            {{ partial "thumbnail" . }}
            <a href="{{.Permalink}}">{{.Title}}</a>
            <p> {{.Description}} </p>
            <br/>
            {{ end }}
        </ul>
        {{ end }}
  </div>
</div>
{{ end }}

This results in error:

render of "section" failed: "C: ...\theme\layouts\docs\list.html:9:23": execute of template failed: template: docs/list.html:9:23: executing "main" at <.Pages.GroupByParam>: error calling GroupByParam: there is no such a 
param

FYI I only encounter this error the first time I start the server.
If I start the server with {{ range .Pages.GroupByParam "myParam" }} given myParam exists in all index.mds it starts fine.
And changing to {{ range .Pages.GroupByParam "group" }} doesn’t raise an error in hot reload.

It’s only when I start the server with {{ range .Pages.GroupByParam "group" }} that I get the above error.

This error occurs when a given section has no matching pages.

So, presuming that doc-a/index.md does not have draft = true, no errors are generated when ranging through the docs/ section.

The problem is that layouts/_default/list.html is used to range through every section. For example, if none of the pages in your other/ section have a group parameter, this error will occur.

The cleanest way to handle this is to create a list template specific to the docs/ section.

mkdir layouts/docs
cp layouts/_default/list.html layouts/docs/

layouts/_default/list.html

{{ range .Pages }}
  <a href="{{.Permalink}}">{{.Title}}</a><br>
{{ end }}

layouts/docs/list.html

{{ range .Pages.GroupByParam "group" }}
  <h3>{{ .Key }}</h3>
  {{ range .Pages.ByWeight }}
    <a href="{{.Permalink}}">{{.Title}}</a><br>
  {{ end }}
{{ end }}
3 Likes

Thanks @jmooring , that’s exactly what was happening.

1 Like

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