Accessing data in .yml file based on the value frontmatter

Hello,

I am trying to output correctly in my views a bio description of the author, based on the current language of the website.

So I have a data/authors.yml file that looks like this:

- matt:
  name: Matt
  bio:
    en: "bio in English"
    ja: "bio in Japanese"
- eric:
  name: Eric
  bio:
    en: "bio in English"
    ja: "bio in Japanese"

in my article frontmatter I have:

author: eric
lang: en

In my article view file, I want to display some info about authors that will depend on the language of the site. So I have at the very beginning of the file. find_author is my helper to point to correct author in .yml file

- author = find_author(current_article.data.author)
- lang = current_page.data.lang

But somehow I keep failing to combine the data to correctly display the bio from .yml file:

#{author.bio.lang}

Try author.bio[:lang]

Hey,

I’ve tried that (%p.description #{author.bio[:lang]}), it doesn’t work (nothing shows up on the view).

When I try just %p.description #{lang} I will see “en” in the view (as the site has currently ENG layout.

Can you share your repo?

Got things to work with a slightly different data scheme (post frontmatter is the same):

- name: Matt
  bio:
    en: "bio in English"
    ja: "bio in Japanese"
- name: Eric
  bio:
    en: "bio in English"
    ja: "bio in Japanese"
- author = data.authors.find { |author| author.name == current_page.data.author.capitalize }
- lang = current_page.data.lang

%p.description #{author.bio[lang]}

Hey, actually your data scheme is the same, the only difference is you’ve removed “:” from the front of “lang” and now it’s working for me :slight_smile:

Thank you!

1 Like

Actually Tom changed the scheme. From:

- matt:
  name: Matt

to:

- name: Matt

Subtle, but important if you cannot find a way to properly read it.

1 Like

Hey great catch! Funnily the old way worked for me but this is way cleaner so I will change to this scheme.
Thanks buddy!