Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering an array in a Liquid/Jekyll template

Tags:

jekyll

liquid

Most Liquid "filters" are actually "map"s in the functional programming sense: you take an array, you apply a function to each element and return the transformed array. I'd like instead to "filter": I want to return only those items in the array that match a certain condition. How can I do that?

Specifically, I'm trying to improve this template:

Authors: 
{% for c in site.data["contributors"] %}
  {% assign contributor = c[1] %}{% if contributor.role contains "author" %}{{contributor.name.given}} {{contributor.name.family}}{% endif %}{% endfor %}

which, prettified, looks like this:

Authors: 
{% for c in site.data["contributors"] %}
  {% assign contributor = c[1] %}
  {% if contributor.role contains "author" %}
    {{contributor.name.given}} {{contributor.name.family}}
  {% endif %}
{% endfor %}

where its data looks like this:

ianbarber:
  name:
    given: Ian
    family: Barber
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - engineer
  homepage: http://riskcompletefailure.com
  google: +ianbarber
  twitter: ianbarber
  email: [email protected]
  description: "Ian is a DRE"

samdutton:
  name:
    given: Sam
    family: Dutton
  org:
    name: Google
    unit: Developer Relations
  country: UK
  role:
    - author
  google: +SamDutton
  email: [email protected]
  description: "Sam is a Developer Advocate"

(example taken from here).

The problem with this approach is that a newline is output if the current element doesn't match the condition, as you can see in https://developers.google.com/web/humans.txt.

How can I fix this?

like image 424
Jacopo Notarstefano Avatar asked Aug 27 '14 08:08

Jacopo Notarstefano


People also ask

Which liquid filters are supported by Jekyll?

All of the standard Liquid filters are supported (see below). To make common tasks easier, Jekyll even adds a few handy filters of its own, all of which you can find on this page. You can also create your own filters using plugins. Prepend baseurl config value to the input to convert a URL path into a relative URL.

How do I add custom filters to Jekyll?

When using Liquid in the context of Jekyll, you have access to Jekyll's custom filters in addition to the standard filters that come with Liquid. If your needs are too specific, you can add new filters through library plugins or you can write your own in Ruby. This, however, is out of the scope of this article.

How do I process a Jekyll template?

Jekyll uses the Liquid templating language to process templates. Generally in Liquid you output content using two curly braces e.g. { { variable }} and perform logic statements by surrounding them in a curly brace percentage sign e.g. {% if statement %}.

How do I use liquid in Jekyll?

Generally in Liquid you output content using two curly braces e.g. { { variable }} and perform logic statements by surrounding them in a curly brace percentage sign e.g. {% if statement %}. To learn more about Liquid, check out the official Liquid Documentation. Jekyll provides a number of useful Liquid additions to help you build your site:


2 Answers

Jekyll 2.0 has a where filter. See docs. Example - from doc:

{{ site.members | where:"graduation_year","2014" }}
{{ site.members | where_exp:"item", "item.graduation_year == 2014" }}
like image 85
spinkus Avatar answered Oct 26 '22 19:10

spinkus


If you want to get rid of the newline if the condition doesn't match, try to remove the unwanted newlines in the if and for clauses:

{% for c in site.data["contributors"] %}{% assign contributor = c[1] %}{% if contributor.role contains "author" %}
  {{contributor.name.given}} {{contributor.name.family}}{% endif %}{% endfor %}

That may not look nice in the source, but it will probably get rid of the newline in the output.

Note: I didn't try this, but similar rearranging helped me get rid of unwanted newlines. You may even have to put the {% if ... on the extreme left, so the indentation does not get included.

like image 45
Rudy Velthuis Avatar answered Oct 26 '22 19:10

Rudy Velthuis