Using where with in/range over all site pages for authorship

I have a site that includes a blog. Each blog post has an authors: array as part of the yaml. I also have a the-team section with individual markdown files for each author. I intentionally did not use taxonomies because I wanted to featured various types of structured information about the team member (ie, sometimes an author) and not just show a list of posts that the author had written.

In my layouts/the-team/single.html I have the following, which works like a charm but has shortcomings:

{{$theAuthor := .Title }}
<h3 class="contributions">Recent Contributions</h3>
      <ul class="recent-contributions">
      {{range .Site.Pages }}
      {{if in .Params.authors $theAuthor }}
      <li><a href="{{.Permalink}}"><i class="icon-{{.Section}}"></i><span class="contribution-title">{{.Title}}</span> <span class="contribution-pubdate"> {{.PublishDate.Format "2006-01-02"}}</span></a></li>

The problem is that some of the team members need their own page but have not necessarily written any posts. With the current solution, team members who have not written any posts still get the “Recent Contributions” headline and an empty <ul>. I’ve tried a half dozen or so combinations of where and range but can’t wrap my head around it. Here are the steps that I need done in the template:

  1. Range over all site pages
  2. Check the site pages authors array
  3. If in the authors:[] array, .Title is present (since .Title is the author’s name for the individual author page), add the matching page to an array
  4. Check that the length of this array is greater than 0 (ie, the individual team-member/author page has at least one post/page written on the site)
  5. If greater than 0, add the “Recent Contributions” headline plus the unordered list with each of the posts where the team-member/author has been given attribution

I’ve tried something like the following but with a few different permutations:

{{$contributions := range where .Site.Pages (where in .Params.authors $theAuthor)}}
{{$contributions := range where .Site.Pages (if in .Params.authors $theAuthor)}}
{{$contributions := range where .Site.Pages (where in .Params.authors $theAuthor)}}
1 Like

The best you can do right now is to loop through twice the way you’re doing it now. Setup a scratch variable that defaults to false, loop thru looking for pages, setting scratch var to true if one is found. Use the scratch var to know whether to print the section at all.

What you really need is a contains operator. Or intersect needs to be extended to work with single items. in won’t work here for you (can’t say “if set is in item”; that doesn’t make sense, it’s backwards).

1 Like

Thanks a lot for the feedback @moorereason I’ll give it a shot later tonight and post whatever solution I come up with using Scratch (i.e., if I figure it out). Cheers.