如何在 JSON+LD head include 中使用许多不同的数据文件更改 forloop 目标

How can I change the forloop target with many different data files in a JSON+LD head include

问题

我有一个 JSON+LD 包含在头部以解析 QA(问题与答案)类型的文章页面。 forloop 目标在每个主题页面上都会发生变化。我无法弄清楚如何改变 forloop 目标以匹配每个页面的数据文件,以便它可以循环遍历带有 QA 的文章的通用 json+ld 脚本中的数据(问题与答案)。

  1. 如果在毒品犯罪页面上,将 drug.yml 文件循环拉入通用 json+LD 进入 <head>
  2. 如果在刑事犯罪页面上,将 crime.yml 文件循环拉入通用 json+LD 进入 <head>

我试过的

{
    "@type": "ItemList",
    "itemListElement": [
  {% for data in site.data.faq.{{page.pagefaq}} %}
      {
        "@type": "Question",
        "name": "{{data.question}}",
        "url": "{{page.url}}/#{{data.id}}",
        "position": "{{ forloop.index }}",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "{{ data.answer}}"
        }
     }{% if forloop.last %}{% else %},{% endif %}
  {% endfor %}],

我现在意识到我不能在 forloop 中插入 frontmatter 变量。然而,该数据文件名在每个页面上都会发生变化,以在 <head>.

中呈现 JSON+LD

forloop 在页面上有效,因为 forloop 目标 yml 是硬编码的。在每个页面的 frontmatter 上,我有:pagefaq:xxxxx。所以,我必须以某种方式告诉 JSON+LD 循环要填充哪个数据文件。

示例 YML 文件

drug.yml

- id: q1
  question: "What is Florida's Marijuana Possesion Law?"
  answer: "In Florida, possession of marijuana without a prescription is a criminal offense. Individuals found to have less than 20 grams on their person may be charged with a misdemeanor, while those found with 20 grams or more of marijuana may be charged with a felony. Medical patients are advised to always have their card and their prescription on them when transporting their medication."
- id: q2
  question: "What is Florida's Oxycodone possesion law?"
  answer: "A prescription painkiller, oxycodone can be found in both OxyContin and Percocet. Individuals found to have less than 7 grams of oxycodone on their person without a prescription may be charged with a third-degree felony. Individuals found to have 7 grams or more may be charged with trafficking."
- id: q3
  question: "What is Florida's Drug Possesion Law?"
  answer: "Drug possession laws aim to limit the possession and personal use of illegal substances. These controlled substances include both Schedule 1 drugs like heroin, LSD, meth, and ecstasy and even prescription drugs like oxycodone and fentanyl.


It’s important to note that marijuana, though allowed for medical use, is still classified as a Schedule 1 drug at the federal level and considered an illegal substance. In Florida, Amendment 2, which was passed in November 2016, allows for the use and cultivation of medical marijuana for qualifying patients. The law does not allow everyone carte blanche access to marijuana. Rather, it requires that patients apply for a medical marijuana card and imposes purchasing and possession limits to ensure that medical marijuana is not abused.


Outside of medical marijuana, one must have a prescription. If you are in possession of marijuana, you can be charged with a misdemeanor or even a felony, depending on the amount of marijuana you have in your possession."

答案来自David Jacquel


您可以像这样使用括号表示法:

{% for data in site.data.faq.[page.pagefaq] %}