# Grouping pages by parameter when parameter is not set

**URL:** <https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946>\
**Category:** support\
**Created:** [February 2, 2022, 11:29pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946 "2022-02-02T23:29:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morteza\_Karimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/morteza_karimi/32/15754_2.png) [@Morteza\_Karimi](https://discourse.gohugo.io/u/Morteza_Karimi)\
**Post date:** [February 2, 2022, 11:29pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/1 "2022-02-02T23:29:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Morteza\_Karimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/morteza_karimi/32/15754_2.png) [@Morteza\_Karimi](https://discourse.gohugo.io/u/Morteza_Karimi)\
**Post date:** [February 3, 2022, 12:23am UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/2 "2022-02-03T00:23:32Z")

</div>

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

```auto
[cascade]
  my_param= "uncategorized"
```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 3, 2022, 12:47am UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/3 "2022-02-03T00:47:13Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [February 3, 2022, 9:30am UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/4 "2022-02-03T09:30:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 3, 2022, 9:40am UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/5 "2022-02-03T09:40:39Z")

</div>

Seems like there is an error…

> [@Morteza\_Karimi](#):
>
> Is there a way to **not** have `.Pages.GroupByParam` **error out**

> [@Morteza\_Karimi](#):
>
> `.Pages.GroupByParam "my_param"` **still errors out**

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 3, 2022, 10:28am UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/6 "2022-02-03T10:28:05Z")

</div>

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

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

> **[Front Matter](https://gohugo.io/content-management/front-matter/)**
>
> Hugo allows you to add front matter in yaml, toml, or json to your content files.

---

<div class="post-metadata">

**Author:** ![Morteza\_Karimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/morteza_karimi/32/15754_2.png) [@Morteza\_Karimi](https://discourse.gohugo.io/u/Morteza_Karimi)\
**Post date:** [February 3, 2022, 5:46pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/7 "2022-02-03T17:46:45Z")

</div>

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

- content

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

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

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

---

<div class="post-metadata">

**Author:** ![Morteza\_Karimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/morteza_karimi/32/15754_2.png) [@Morteza\_Karimi](https://discourse.gohugo.io/u/Morteza_Karimi)\
**Post date:** [February 3, 2022, 7:07pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/8 "2022-02-03T19:07:42Z")

</div>

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.md`s 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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 3, 2022, 8:05pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/9 "2022-02-03T20:05:30Z")

</div>

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.

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

```

layouts/\_default/list.html

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

```

layouts/docs/list.html

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

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Morteza\_Karimi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/morteza_karimi/32/15754_2.png) [@Morteza\_Karimi](https://discourse.gohugo.io/u/Morteza_Karimi)\
**Post date:** [February 4, 2022, 11:12pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/10 "2022-02-04T23:12:49Z")

</div>

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

---

<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:** [February 6, 2022, 11:13pm UTC](https://discourse.gohugo.io/t/grouping-pages-by-parameter-when-parameter-is-not-set/36946/11 "2022-02-06T23:13:35Z")

</div>

Closed on February 6, 2022, 11:13pm UTC

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