Muut
March 28, 2015, 7:35am
1
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!
Muut
March 28, 2015, 2:37pm
2
What is your authors file called, where is it located and what is the contents of the file?
Muut
March 28, 2015, 2:41pm
3
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.
Muut
March 28, 2015, 2:57pm
4
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.
Muut
March 28, 2015, 3:04pm
5
Thanks, this is the content of the yaml in proper format:
kaladan:
name: kaladan
email: kaladan@mail
image: kaladan.png
bio: simple bio
---
Muut
March 28, 2015, 3:05pm
6
That looks right. Oh!!! Hold on
Muut
March 28, 2015, 3:05pm
7
Need to move to a computer can’t write code on an iPad
Muut
March 28, 2015, 3:20pm
8
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"
]
]
---
Muut
March 28, 2015, 3:21pm
9
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 %}
Muut
March 28, 2015, 3:27pm
10
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 }}
Muut
March 28, 2015, 3:28pm
11
BTW had a typo on that first image example email.image
should obviously been author.image
. Waited too long to fix.
Muut
March 28, 2015, 3:29pm
12
Hey, thanks! I’ll try this and write back Have a nice weekend!
Muut
March 28, 2015, 8:01pm
13
It works! Thank you very much