Search code examples
jekyllliquidjekyll-theme

How can I limit and filter a collection by key value in a jekyll theme?


I'm trying to output one item from a collection where sticky: "true".

I've tried Getting a specific item from a collection in Jekyll From collection _defense/dui.html


---
layout: right-sidebar
title: "Drug Offense"
crumbtitle: "drug-related crime"
permalink: "/practice-law/criminal-law/drug-offense.html"
sticky: true
twitter: "Working on this"

facebook: "Civil Litigation can cost you hundreds if not thousands of dollars if you are not adequately protecting your rights. Civil Litigation can be anything from traffic tickets, hurricane insurance claims to medical malpractice. Call or write for a free, no obligation review of your situation."

web: "An arrest for a drug offense can have significant consequences. Drug offense charges can range from possession to trafficking and can carry significant penalties, including minimum mandatory prison sentences.


The Prosecutor’s process of deciding what if any criminal charges to file can be a very critical stage in the case. This process happens very early in a case and as your lawyers, we can impact this decision.


**DO NOT** make a statement to law enforcement without consulting us first. Call Cagan & Cagan today for a free consultation."

In collection page _practices/criminal-defense.html

    <!-- One -->
{% assign defenses = site.defense | where:"true", "page.sticky" | limit:1 %}
{% assign defense = defense[0] %}
{{ defense | inspect }}
    <section class="wrapper style4 container">
        <div class="row gtr-150">
            <div class="col-8 col-12-narrower">
                <!-- Content -->
                <div class="content">
                    <section>
                        <header>
                            <a href="#" class="image featured">
                                <h2>{{ defense.title }}</h2>
                            </a>
                        </header>
                        <p>{{ defense.web | markdownify }}</p>
                    </section>
                </div>
        </div>

I get nil value right now. I want the first item with sticky true and end there.


Solution

  • where filter syntax is : {% assign res = array | where: "key", "expected value" %}

    but, you've reversed arguments order : {% assign res = hash | where: "expected value", "key" %}

    see jekyll documentation here

    So, you can replace

    {% assign defenses = site.defense | where:"true", "page.sticky" | limit:1 %}
    {% assign defense = defense[0] %}
    

    by

    {% assign defense = site.defense | where: "sticky", "true" | first %}
    

    notes :

    Edit : if you want to get some items from posts, pages or collection, depending on a front matter variable, you can do :

    {% assign items = site.anycollection | where: "any_key", "string_value" %}
    

    You can then print anything from this resulting array, using a for loop and, eventually limit and offset parameters.

    {% for item in items limit:2 %}
      and so on ....