Search code examples
jsonliquidstructured-datajekyll-theme

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


Problem

I have one JSON+LD include in the head to parse QA (Questions & Answers) type article pages. The forloop target changes on every topic page. I cannot figure out how to variable the forloop target to match the data file for each page so it can loop through the data in the universal json+ld script for articles with QA ( Questions & Answers).

  1. If on drug offense page, pull the drug.yml file loop into the universal json+LD into <head>.
  2. If on criminal offense page, pull the crime.yml file loop into the universal json+LD into <head>.

What I've Tried

{
    "@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 %}],

I realize now that I cannot insert a frontmatter variable in the forloop. Yet, that data file name changes on each page to render the JSON+LD in the <head>.

The forloop works on the page because the forloop target yml is hardcoded. On each page frontmatter, I have: pagefaq:xxxxx. So, I must somehow be able to tell the JSON+LD loop which datafile to populate.

Sample YML File

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."

Solution

  • The answer comes from David Jacquel Can a liquid for loop contain a page variable in Jekyll?


    You can use the bracket notation like this :

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