frontend - 如何创建和显示自定义 Jekyll 元数据
问题描述
我有一个 Jekyll 私人博客(即实验室笔记本),它使用了极好的最小错误主题。我为我的每篇博客文章大量使用标签,并且可以看到标签列表。
我也喜欢跟踪我在博客文章中提到的人,所以我为每篇文章添加了额外的元数据,如下所示:
---
title: "My great experiment"
tags:
- physics
- chemistry
- Higgs boson
people:
- Albert Einstein
- Galileo Galilei
- Marie Curie
---
我想做的是查看我博客中提到的所有人——在所有博客文章中——但 Jekyll 似乎没有填充site.people
变量。我试图site.people
像这样可视化变量:
{
{
"people": [
{%- for person in site.people -%}
"{{ person[0] }}"{% unless forloop.last %},{% endunless %}
{%- endfor -%}
],
}
}
但我在呈现的网页上看到的只是:
{ { “people”: [],
} }
我需要做什么才能让 Jekyll 看到人员元数据?我需要以某种方式告诉 Jekyll 还有其他需要查看的元数据吗?
解决方案
我认为您需要在第二个代码片段中替换site
为page
,请参阅变量处理。
- 站点:全球网站(例如_config.yml)
- 页面:当前页面
另外我删除了数组索引[0]
。
{
{
"people": [
{%- for person in page.people -%}
"{{ person }}"{% unless forloop.last %},{% endunless %}
{%- endfor -%}
],
}
}
我的输出/结果
{ { “people”: [“Albert Einstein”,”Galileo Galilei”,”Marie Curie”],
} }
2020 年 10 月 23 日更新:
_config.yml
声明所有物理学家的全局片段。
...
physicists:
- Albert Einstein
- Galileo Galilei
- Marie Curie
- Isaac Newton
- Nikola Tesla
- Max Planck
...
获取任何帖子上所有物理学家的列表
---
title: "My great experiment"
tags:
- physics
- chemistry
- Higgs boson
people:
- Albert Einstein
- Galileo Galilei
- Marie Curie
---
## List all Physicists
{% for itm in site.physicists %}
{{ itm }}
{% endfor %}
推荐阅读
- excel - 多列的文本到列 - Excel VBA
- excel - 尝试将 array(I,1).value 分配给昏暗字符串类型变量时出现错误 424 vba
- javascript - Vue.js async/await with then 函数未执行
- php - PHP提交空白屏幕-脚本未在POST上运行?
- jquery - Bootstrap 4 popper 在滚动或窗口调整大小时移动左上角
- php - 如何将 json 文件转换为图形?
- assembly - x86-64:LEA 指令类型
- kubernetes - k8s namspaces always in Terminating
- c# - 启动过程以打开本地 html 文件导致访问被拒绝
- node.js - 如何防止用户通过输入 URL 直接访问我的反应应用程序的页面,并将他们重定向回登录页面