Including content types dynamically in pagination

Hello folks,

in most of my themes I added a default content type called posts. This works fine in most cases. Therefore, I hardcoded the used content types in the pagination as follows:

{{ $paginator := .Paginate (where .Site.Pages "Type" "post") }}

Now, a user requested that he uses different stylings depending on the content type. This custom content types should also be listed in the pagination.

To give users more flexibility I thought about adding an array of strings in the config file that contains all other content types that should be added to the pagination.

Does someone knows an elegant approach to merge the posts of all content types in the $paginator variable?

Thanks for your help.

1 Like

How about something like this?

{{ $paginator := .Paginate (where .Site.Pages "Type" "in" .Site.Params.myposttypes) }}

Edit: I think in must be a quoted string.

The combination with the template function in is a good idea. I’ll give it a shot.

I’ve tried the your snippet but the comparison, if the type is in the array of custom content types, is always false. Therefore, the paginator doesn’t contain any pages.

In my config.toml I created an array of custom content types:

custom_content_types = ["projects", "post"]

The content folder contains both content types that I listed above:

content
├── post
│   ├── creating-a-new-theme.md
│   ├── go-is-for-lovers.md
│   ├── hugo-is-for-lovers.md
│   ├── introducing-icarus-and-its-features.md
│   ├── linked-post.md
│   ├── markdowntest.md
│   └── migrate-from-jekyll.md
└── projects
    └── foobar.md

Currently, the paginator only looks for files of the type “post”:

{{ $paginator := .Paginate (where .Site.Pages "Type" "post") }}
    
{{ range $paginator.Pages }}
    Is in array: {{ in .Type .Site.Params.custom_content_types }}<br>
{{end }}

As you can see I checked if in returns true if the type “post” is in custom_content_types (which is the case). But in always returns false.

Is in array: false
Is in array: false
Is in array: false
Is in array: false
Is in array: false
Is in array: false
Is in array: false

Do you have any idea why this is the case?

1 Like

Your in syntax is backwards. Try this:

{{ in .Site.Params.custom_content_types .Type }}

FWIW, I agree with your logic: the in syntax is the opposite of what I think it should be.

{{ in arg1 arg2 }}  should be equivalent to {{ arg1 in arg2 }}

But, it’s not. :frowning:

As I said before, this makes sense if you think of these as functions and not as keywords.

That’s my problem. I think of in as an operator such as eq and lt.

@moorereason and @bep, thanks for your corrections. Maybe I thought the same as @moorereason. Now the output is true.

But I still have the problem that no pages will be added to the paginator.

I tried use the in as a function:

(where .Site.Pages "in" "Type" .Site.Params.custom_content_types)

but where can’t handle functions as first parameter. I get the following error message:

ERROR: 2016/03/08 15:37:29 general.go:203: Error while rendering homepage: template: theme/index.html:6:32: executing “theme/index.html” at <where .Site.Pages "i…>: error calling where: in isn’t a field of struct type *hugolib.Page

Did anyone resolve the correct way to include more than one type in the where clause for .Paginate?

{{ $paginator := .Paginate (where .Data.Pages “Type” “post”) }} --> one type

would like to look for “post” and “somethingelse”