Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

For not working with external yaml file

Started by Muut Archive 11 years ago · 12 replies · 357 views
11 years ago

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

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

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

Thanks!

11 years ago

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

11 years ago

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.

11 years ago

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.

11 years ago

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

YAML

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

Need to move to a computer can't write code on an iPad :)

11 years ago

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

JS

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"
  ]
]
---
11 years ago

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

YAML
authors:
   - name: kaladan
     email: kaladan@mail
     image: kaladan.png
     bio: simple bio
   - name: Joe Blow
     email: j[email protected]
     image: joe.jpg
     bio: joe bio

Then use this code to loop over it:

TWIG
{% 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 %}
11 years ago

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

YAML
kaladan:
  name: kaladan
  email: kaladan@mail
  image: kaladan.png
  bio: simple bio
joeblow:
  name: Joe Blow
  email: j[email protected]
  image: joe.jpg
  bio: joe bio

and

TWIG
{% 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:

TWIG
Joe's Email: {{ config.author.joeblow.email }}
11 years ago

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

11 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1323 9 years ago
Archive · by Muut Archive, 9 years ago
2 920 9 years ago
Archive · by Muut Archive, 9 years ago
2 4048 9 years ago
Archive · by Muut Archive, 9 years ago
1 2924 9 years ago
Archive · by Muut Archive, 9 years ago
3 1106 9 years ago