首页 > 解决方案 > 如何在 jekyll 主题中按键值限制和过滤集合?

问题描述

我正在尝试从粘性集合中输出一个项目:“true”。

我试过从 Jekyll From 集合中的集合中获取特定项目_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>

我现在得到零值。我想要第一个带有粘性的项目并在那里结束。

标签: jekyllliquidjekyll-theme

解决方案


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

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

在此处查看 jekyll 文档

所以,你可以更换

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

经过

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

笔记:

  • first获取数组的第一个元素,替换limit:1 > array[0].
  • limit是一个for回路控制结构参数。您不能在assign标签中使用它。

编辑:如果你想从帖子、页面或收藏中获取一些项目,取决于前端变量,你可以这样做:

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

然后,您可以使用 for 循环和最终使用参数从这个结果数组中打印limit任何offset内容

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

推荐阅读