For not working with external yaml file

Hi, i’m working on a pet media project and i’m trying to create an yaml file with all the authors in the media. I’m having trouble using “for”:

      {{ config.authors.kaladan.name }} << this is working

      {% for author in config.authors.kaladan.name %} {{ author }} {% endfor %} << this is not

Thanks!

What is your authors file called, where is it located and what is the contents of the file?

Hi,

the file is called authors.yaml and it is located in /user/config . The content of the file is:

kaladan:
name: kaladan
email: kaladan@mail
image: kaladan.png
bio: simple bio

Thanks.

Try turning on the debug panel then putting {{ dump(config.authors) }} in the page. Then check the messages panel and expand the results.

I have a hunch you just have poorly formatted yaml. You would need to paste your yaml file between 2 sets of 3-dashes for it to display properly in this forum.

Thanks, this is the content of the yaml in proper format:

kaladan:
    name: kaladan
    email: kaladan@mail
    image: kaladan.png
    bio: simple bio
---

That looks right. Oh!!! Hold on

Need to move to a computer can’t write code on an iPad :slight_smile:

I have activated the debug panel and it looks that the yaml file works ok:

array:2 [
  "kaladan" => array:4 [
    "name" => "kaladan"
    "email" => "kaladan@mail"
    "image" => "kaladan.png"
    "bio" => "simplebio"
  ]
  "ivgin" => array:4 [
    "name" => "ivgin"
    "email" => "ivgin@mail"
    "image" => "ivgin.png"
    "bio" => "ivgin simple bio"
  ]
]
---

First off you want to change your format of authors.yaml to something like this:

authors:
   - name: kaladan
     email: kaladan@mail
     image: kaladan.png
     bio: simple bio
   - name: Joe Blow
     email: joe@blow.com
     image: joe.jpg
     bio: joe bio

Then use this code to loop over it:

{% for author in config.authors.authors %}
<h1>{{ author.name }}</h1>
<img src="{{ email.image}}" /> 
<ul>
  <li>email: {{ author.email }}</li>
  <li>bio: {{ author.bio }}</li>
</ul>
{% endfor %}

Actually you can use the format you originally had also as long as only authors are in the file:

kaladan:
  name: kaladan
  email: kaladan@mail
  image: kaladan.png
  bio: simple bio
joeblow:
  name: Joe Blow
  email: joe@blow.com
  image: joe.jpg
  bio: joe bio

and

{% for author in config.authors %}
<h1>{{ author.name }}</h1> 
<img src="{{ author.image}}" />
<ul>
  <li>email: {{ author.email }}</li>
  <li>bio: {{ author.bio }}</li>
</ul>
{% endfor %}

It does also have the benefit of being able to extract info via the author name:

Joe's Email: {{ config.author.joeblow.email }}

BTW had a typo on that first image example :slight_smile: email.image should obviously been author.image. Waited too long to fix.

Hey, thanks! I’ll try this and write back :slight_smile: Have a nice weekend!

It works! Thank you very much :slight_smile: