Trying to use a filtered section of YML in a shortcode

Hey folks, I have this YML file…

section:
 - name: a
 - questions:
    - question: question 1a
      answer: answer 1a
    - question: question 2a
      answer: answer 2a
    - question: question 3a
      answer: answer 3a
section:
 - name: b
 - questions:
    - question: question 1b
      answer: answer 1b
    - question: question 2b
      answer: answer 2b
    - question: question 3b
      answer: answer 3b
section:
 - name: c
 - questions:
    - question: question 1c
      answer: answer 1c
    - question: question 2c
      answer: answer 2c
    - question: question 3c
      answer: answer 3c

My goal is to be able to create a shortcode such that, in various content pages, I can say "{{<faq “a”>}} and have the ‘a’ section of questions listed out. The YML file is in my data folder, and I can access it, but I’m not sure how to select the specific set of questions I want (assume the YML is being added to, new sections being created, etc. without any modification to the shortcode)

I also considered having a /data/faq folder, and then a.yml, b.yml, c.yml … but I wasn’t sure how to access $.Site.Data.faq.

I’d restructure the YAML file

data/faqs.yaml
a:
  - question: question 1a
    answer: answer 1a
  - question: question 2a
    answer: answer 2a
  - question: question 3a
    answer: answer 3a
b:
  - question: question 1b
    answer: answer 1b
  - question: question 2b
    answer: answer 2b
  - question: question 3b
c:
  - question: question 1c
    answer: answer 1c
  - question: question 2c
    answer: answer 2c
  - question: question 3c
    answer: answer 3c

layouts/shortcodes/faq.html

{{ range index site.Data.faqs (.Get 0) }}
  Question: {{ .question }}<br>
  Answer: {{ .answer }}<br><br>
{{ end }}

markdown

{{< faq a >}}
1 Like

Thanks so much, that worked perfectly!

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.