Liquid Limit

a tenacious unwanted comma

Oct 23, 2013

This is the story of how I finally hit a Liquid limitation that I couldn't work around with a series of ugly hacks.

Last night at MaptimePDX I decided to add a Leaflet map to my blog, just for fun. It would show markers for all posts with a location field. An example post:

---
title: Locatable Post
permalink: with-location/
location:
  lon: 0.5
  lat: 102.0
---

This post has a location field!

For the map, I needed to aggregate all the location data into a single GeoJSON index. My first attempt:

```{% raw %}


{ "type": "FeatureCollection", "features": [{% for post in site.posts %}{% if post.location %} { "type": "Feature", "geometry": {"type": "Point", "coordinates": [{{ post.location.lon }}, {{ post.location.lat }}]}, "properties": { "title": "{{ post.title }}", "url": "{{ post.url }}" } }{% unless forloop.last %}, {% endunless %}{% endif %}{% endfor %} ] } {% endraw %}```

Result:

{ "type": "FeatureCollection",
  "features": [
    { "type": "Feature",
      "geometry": {"type": "Point", "coordinates": [0.5, 102.0]},
      "properties": {
        "title": "Locatable Post",
        "url": "/with-location/"
        }
      }, 
    ]
  }

Unfortunately the output still has a trailing comma, which isn't valid JSON. This is because the last location-having post isn't the last post in the collection. I'm hacking around the lack of collection filtering in Liquid by nesting an if tag inside the for tag, which breaks forloop.last.

The above experience is representative of using Liquid: inconsistent, ad hoc language semantics get in the way and generally increase the cognitive cost of using it.

Granted, I could implement the semantics needed to drop my trailing comma as a Jekyll plugin, but this wouldn't change the root problem: Liquid is a terrible templating language.

Right now I'm investigating moving to DocPad, however given its chronic issues with memory usage, I may go with something more basic.