如何在jekyll主题中按键值限制和过滤集合?

时间:2019-07-02 15:19:25

标签: jekyll liquid jekyll-theme

我正在尝试从粘性为“ true”的集合中输出一项。

我已经尝试过Getting a specific item from a collection in Jekyll 来自集合_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."

在收藏页_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>

我现在得到零价值。我要第一项内容为true,并在此处结束。

1 个答案:

答案 0 :(得分:1)

where过滤器语法为: {% assign res = array | where: "key", "expected value" %}

但是,您已经颠倒了参数order: {% assign res = hash | where: "expected value", "key" %}

see jekyll documentation here

因此,您可以替换

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

作者

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

注释:

编辑:如果要从帖子,页面或收藏集中获取某些项目,则可以根据前题变量进行操作:

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

然后,您可以使用for循环并最终使用limit and offset parameters打印此结果数组中的任何内容。

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