Is there an equivalent of .Site.Data for page scope?

Hi, is there an equivalent of .Site.Data for page scope?
I want to place some data files to page folder, such as:

pages/
  foo/
    comments/
      a.json
      b.json
      ...

Is it possible to get the foo/comments data in the foo page like .Data.comments?
I’m able to get the content via page resources, but I don’t know how to parse it to a map.

content/pages/foo/comments/a.json

{
  "author": "John Doe",
  "date": "2022-09-17T13:47:25-07:00",
  "body": "This is a <strong>bold</strong> word in a comment."
}

template

{{ with .Resources.Match "comments/*.json" }}
  <h3>Comments</h3>
  {{ range . }}
    {{ $d := .Content | transform.Unmarshal }}
    <p>
      Author: {{ $d.author }}<br>
      Date: {{ $d.date | time.AsTime | time.Format ":date_long" }}<br>
      Body: {{ $d.body | safeHTML }}
    </p>
  {{ end }}
{{ end }}
1 Like

Thanks a lot, one more question is that, would this become a performance issue if there are a lot of comment files, compare to .site.data approach.

I have no idea how the two approaches compare.

In my example:

  • With 1 data file the build time was 55 ms (average of 5 runs)
  • With 300 data files the build time was 170 ms (average of 5 runs)

So I think the comparison is irrelevant.

1 Like

I ran another test, moving the 300 data files to the data directory (accessed via site.Data.comments). The build time was essentially the same at 176 ms (average of 5 runs).

1 Like

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