如何在 jekyll 主题中通过键值限制和过滤 collection?
How can I limit and filter a collection by key value in a jekyll theme?
我正在尝试从 collection 中输出一项,其中粘性:"true"。
我试过了
来自 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."
在 collection 页中 _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>
我现在得到的是零值。我想要第一个带有 sticky true 的项目并在那里结束。
where
过滤器语法为:
{% assign res = array | where: "key", "expected value" %}
但是,您颠倒了参数顺序:
{% assign res = hash | where: "expected value", "key" %}
所以,你可以替换
{% 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
loop control structure parameter。您不能在 assign
标签中使用它。
Edit :如果你想从帖子、页面或集合中获取一些项目,取决于前面的变量,你可以这样做:
{% assign items = site.anycollection | where: "any_key", "string_value" %}
然后您可以使用 for 循环从这个结果数组打印任何内容,最终 limit
and offset
parameters.
{% for item in items limit:2 %}
and so on ....
我正在尝试从 collection 中输出一项,其中粘性:"true"。
我试过了_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."
在 collection 页中 _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>
我现在得到的是零值。我想要第一个带有 sticky true 的项目并在那里结束。
where
过滤器语法为:
{% assign res = array | where: "key", "expected value" %}
但是,您颠倒了参数顺序:
{% assign res = hash | where: "expected value", "key" %}
所以,你可以替换
{% 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
loop control structure parameter。您不能在assign
标签中使用它。
Edit :如果你想从帖子、页面或集合中获取一些项目,取决于前面的变量,你可以这样做:
{% assign items = site.anycollection | where: "any_key", "string_value" %}
然后您可以使用 for 循环从这个结果数组打印任何内容,最终 limit
and offset
parameters.
{% for item in items limit:2 %}
and so on ....