Can I use multiple data files to create "n" comparison pages?

Say I have a list of products, like so:

a.json
b.json
c.json
d.json

Is it possible to create a page for every combination of file(s)? ie:

  • /a-vs-b/
  • /a-vs-c/
  • /a-vs-d/
  • /b-vs-c/
  • /b-vs-d/
  • /c-vs-d/

And (bonus) three-way comparisons, ie:

  • /a-vs-b-vs-c/
  • /a-vs-b-vs-d/
  • /a-vs-c-vs-d/
  • etc…

Logically, the list can be generated in a similar manner as:

{{ $allPages := .Pages }}
{{ range $index, $page1 := .Pages }}
  {{ $subsequentPages := (after (add $index 1) $allPages) }}
  {{ range $page2 := $subsequentPages }}
    <p>{{ $page1.Title }} vs {{ $page2.Title }}</p>
  {{ end }}
{{end}}

I can see there’s a discussion / branch here, but I get the impression that this is more related to remote data.

I think this goes some way to solving my problem, however I’m more interested in creating simple logic that then triggers the creation of a new page. An example of this triggering mechanism might be:

{{ $allPages := .Pages }}
{{ range $index, $page1 := .Pages }}
  {{ $subsequentPages := (after (add $index 1) $allPages) }}
  {{ range $page2 := $subsequentPages }}
    {{ renderPage "comparisonTemplate" (dict "left" $page1 "right" $page2) }}
  {{ end }}
{{end}}

At this point, Hugo is not able to create pages from template, only from the command line (ie hugo new blog/post-one.md).

{{ range .Pages }} iterates over existing pages.

I’ve seen somewhere how someone automated/scripted the creation of md files for Hugo from json content though, you could try searching for those.

I’d be OK with a scripted approach, it’d be a shame to lose the live-reload during development and additional overhead of running scripts with every change. I guess I could use a task-runner to handle this.

As an aside, I realised why I thought this might be doable - Hugo’s .Paginate system does in fact generate new pages based on internal logic. It generates /page/n depending on the number of pages, and the PageSize.

Pagination is not a general “page generator” – but what you want will eventually happen.

1 Like

Thanks for clarifying. I’ve decided for the interim to take this approach:

  1. To assume this feature will eventually happen (thanks @bep)
  2. Store my data in the /data/ directory
  3. Create a shortcode that accepts two data paths (left and right) and outputs my comparison page
  4. Use a script to create section content for all data-combinations, the content will simply use the shortcode.
---
title: "A vs B"
---
{{< comparison left="data/a.json" right="data/b.json" >}}

With this approach I would get livereload when editing the data files, and it should be easy enough to change my theme if / when a “scripted page generator” becomes available.

Hi, have you finish your comparison page? Can I have a look at this comparison page?