Skip Navigation code drift

First & Last In Tag Lists

written
in TIL, but also Code

As part of relaunching Codedrift on the Ghost platform, I wanted to make better looking tag sentences. This is the code that powers the tag segments in the "in X, but also Y, Z, & Q" format.

There's an extra comma when the tag count is exactly three, but there's no easy way to fix that issue without access to a .count property in Ghost.

{{#foreach tags}}
  {{!-- first tag --}}
  {{#if @first}}
    {{#if @last}}
      {{!-- Only tag in the collection --}}
      in <a href="{{url}}">{{name}}</a>
    {{/if}}
    {{#unless @last}}
      {{!-- first of at least two --}}
      in <a href="{{url}}">{{name}}</a>, but also
    {{/unless}}
  {{/if}}

  {{!-- last tag --}}
  {{#if @last}}
    {{#match @number "=" 2}}
      {{!-- (tagX, but also) tagY --}}
      <a href="{{url}}">{{name}}</a>
    {{/match}}
    {{#match @number ">" 2}}
      {{!-- (tagX, but also tagA...) & tagY --}}
      &amp; <a href="{{url}}">{{name}}</a>
    {{/match}}
  {{/if}}

  {{!-- Not first, and not last --}}
  {{#unless @first}}
    {{#unless @last}}
      <a href="{{url}}">{{name}}</a>,
    {{/unless}}
  {{/unless}}

{{/foreach}}