Adding comments to bookshelves

I’m wondering if it’s possible to use a data template to add comments to my bookshelf page for (some of) the books I’ve finished reading. Here’s the relevant excerpt from my bookshelf (I’ve removed references to “class” so as to simplify):

{{ range .Site.Data.bookshelves.finishedreading }}
     <p>
      <a href="https://micro.blog/books/{{ .isbn }}">
     <img src="{{ .cover_url }}" alt="{{ .title }} cover" width="100" />
    {{ .author}}, <cite>{{ .title }}</cite></a></p>

{{ end }}

What I’d like to do is add a nested range which would loop through my JSON data and, where the “bktitle” from the data matches the current .title, to insert the corresponding "bkcomment”. Something along these lines:

{{ $booknote := site.Data.booknotes }}
{{ range .Site.Data.bookshelves.finishedreading }}
{{ $booktitle := printf "s" .title }}
   <p>
   <a href="https://micro.blog/books/{{ .isbn }}">
         <img src="{{ .cover_url }}" alt="{{ .title }} cover" width="100" />
             {{ .author}}, <cite>{{ .title }}</cite></a>
    {{ range $booknote.mybooks }}
        {{ if eq .bktitle $booktitle }}
            <br>{{ .bkcomment }}
        {{ end }}
    {{ end }}
    </p>

{{ end }}

And, finally, the JSON data looks like this:

{
    "mybooks": [
        {
            "bktitle": "Judas 62",
            "bkcomment": "Mission: Impossible meets Smiley’s People. Who said the Cold War is over? Reviewing Box 88, I said it was “a secret, rogue intelligence-and-assassination outfit”. I was wrong: we learn here that they stop short of assassination. Highly enjoyable."
        },
        {
            "bktitle": "I Know What I Saw",
            "bkcomment": "The first-person narrative of a long-term homeless man whose memory is damaged and who is sure he witnessed a murder that can’t have happened when he says it did. The brilliance of the start isn’t maintained."
        }
    ]
}

Can this (or something like it) be made to work and, if so, will it cause any problems if I update the JSON data each time I add a book and/or comment?

I don’t see why it would cause a problem. I think the only thing to know is that adding book to a shelf does not trigger a site build. Because I think you’re updating a data file in your theme, that would trigger a site a build.

Thank you for this. I think I’ll try it and see what happens.

I got this working :sunglasses: but I had to tweak it a bit. Until I replaced the line {{ if eq .bktitle $booktitle }} the page wouldn’t display any books at all. Now, that part of the template reads:

{{ $booknote := site.Data.booknotes }}
{{ range .Site.Data.bookshelves.finishedreading }}
{{ $booktitle := printf "s" .title }}
   <p class="bookshelf_generated">
   <a href="https://micro.blog/books/{{ .isbn }}">
         <img src="{{ .cover_url }}" alt="{{ .title }} cover" class="bshelf" width="100" />
             {{ .author}}, <cite>{{ .title }}</cite></a>
    {{ range $booknote.mybooks }}
       {{ $bktitle := printf "s" .bktitle }}
        {{ if eq $bktitle $booktitle }}
            <br>{{ .bkcomment }}
        {{ end }}
    {{ end }}
    </p>

{{ end }}