从Jekyll的集合中获取特定物品 [英] Getting a specific item from a collection in Jekyll

查看:56
本文介绍了从Jekyll的集合中获取特定物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Jekyll重建我公司的当前站点,并尝试使用具有属性(YAML前端的键/值对)的对象(集合中的文件)围绕内容模型建立结构.简单的概念性内容旨在向我的团队演示有效的内容建模.

I'm rebuilding my company's current site in Jekyll and attempting to set up the structure around a content model using objects (files in a collection) that have attributes (key/value pairs in YAML front-matter). Simple conceptual stuff designed to demonstrate effective content modeling to my team.

站点上任何可重用的东西都将成为对象,其对象类型由包含其文件的特定集合定义.因此,对于公司提供的服务,我有一个服务"集合,一个客户"集合和一个人"集合,每个人都有一个文件.

Anything on the site that gets reused becomes an object, with the type of object defined by the specific collection that contains its file. So, I have a "services" collection for services offered by the company, a "clients" collection, and a "people" collection with a file for each person.

我遇到的问题是引用集合中的特定项目.例如,我希望我的帖子有作者.我已经找到了很多使用_data的解决方案,但是我希望我的作者拥有页面,并且集合会自动为每个人输出一个页面.我已经能够获得_data来生成页面,但是我无法控制项目的列出顺序,而对于集合,我可以通过很多方法来控制其顺序.

The problem I'm having is referencing a specific item in a collection. For example, I want my posts to have authors. I've found a ton of solutions for this using _data, but I want my authors to have pages and collections automatically outputs a page for each person. I've been able to get _data to generate pages, but I have no control over the order in which items are listed, whereas with a collection there are a lot of ways for me to control the order.

我当前的解决方案感觉很棘手.我给每个人一个ID,它等于"firstname-lastname",因此在YAML的前题中会说id: steve-hickey.然后,我使用以下代码遍历网站上的每个人,并返回与帖子中指定的author-id相匹配的任何人.

My current solution feels hacky. I'm giving each person an id which is equal to "firstname-lastname" so in the YAML front-matter it would say id: steve-hickey. Then I use the following code to loop through every person on the site and return any that match the author-id specified in the post.

帖子布局模板:

---
layout: default
---

<header class="intro post-header">
  <h1>{{ page.title }}</h1>
  {% assign people = site.people | where:"id", page.author-id %}
  {% for person in people %}
  <p>Written by <a href="/about/{{ page.author-id }}/">{{ person.first-name }} {{ person.last-name }}</a> on <p>{{ page.date | date: '%B %d, %Y' }}</p></p>
  {% endfor %}
</header>
<div class="post-body">

{{ content }}

</div>

帖子的重点:

---
title: User Offboarding
layout: post
nav-area: blog
author-id: steve-hickey
---

人集合中的人文件:

---
id: steve-hickey
first-name: Steve
last-name: Hickey
job: User Experience Strategist & AUX Director
layout: person
nav-area: about
portrait-url:
---

似乎应该有一种方法可以根据已经拥有的唯一属性(例如名称或url)来识别特定文件或对象.这样,我就可以指向一个特定的对象,而不必对所有对象进行评估以获取我必须编写的唯一属性.但是经过3天的搜索,我找不到很好的答案.

It seems like there should be a way to identify a specific file or object based on a unique attribute that it already possesses, such as its name or url. That way I can just point to a specific object instead of evaluating all of them for a unique property I have to write. But after 3 days of searching I can't find a good answer to this.

推荐答案

如果人在您的集合中是唯一的,则{% assign author = site.people | where:"id", page.author-id %}将返回一个包含一个元素的数组. 为了直接访问此元素,请执行以下操作:

If people are unique in your collection {% assign author = site.people | where:"id", page.author-id %} will return an array with one element. In order to directly access this element do :

{% assign author = site.people | where:"id", page.author-id  | first %}

这将获取数组中的第一个(也是唯一一个)元素.现在,您直接进行{{ author.anykey }}.

This grab the first (and only) element in you array. You now do {{ author.anykey }} directly.

这篇关于从Jekyll的集合中获取特定物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆