Accessing Global Data File #3767
-
I have a yaml data file at author-name:
full_name: First Last
display_name: Display Name
avatar: avatar.jpg
bio: "I am an author" I also have a nunjucks template at ---
layout: base
pagination:
data: authors
size: 1
alias: author
permalink: /blog/authors/{{ author | slug }}/
---
<article>
{{ authors | log }}
{{ author | log }}
{{ content | safe }}
</article> The authors log at author.njk returns an empty object I need access to the specific authors entry in the yaml data file for this template. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Looking at the 11ty docs on Global Data, it seems that only json files or javascript that returns data can be used this way. I'd suggest adding an authors.js file to your _data directory and add a script that returns the result of converting the YAML to json. |
Beta Was this translation helpful? Give feedback.
-
I wasn't aware of custom data. Thanks for educating me. |
Beta Was this translation helpful? Give feedback.
-
Hi @gabidombrowski, I think you need to loop over the collection with something like: <ul>
{%- for author in authors %}
<li>{{ author }}</li>
{% endfor -%}
</ul>
|
Beta Was this translation helpful? Give feedback.
-
I did some digging and testing and came up with the following to make pagination work with the YAML file (though I had to alter the YAML...see below. First, I changed the YAML file to this: - full_name: Gabi Dombrowski
display_name: Gabi Dombrowski
avatar: gabi_avatar.jpg
bio: "I am one author"
- full_name: Bob Monsour
display_name: Bob Monsour
avatar: bob_avatar.jpg
bio: "I am another author" Then, as you noted, I added the following lines to the eleventy config file to support YAML as a custom data format: import yaml from "js-yaml";
eleventyConfig.addDataExtension("yml, yaml", (contents) =>
yaml.load(contents)
); Then, in the authors.njk, I changed it to this: ---
layout: base.njk
pagination:
data: authors
size: 1
alias: author
permalink: /blog/authors/{{ author.full_name | slugify }}/
--- <article>
{{ author.full_name }}
{{ author.bio }}
{# add more here as needed #}
</article> Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Turns out that the 11ty pagination returns keys by default. I'm not sure why I can't access the In ---
layout: base
pagination:
data: authors
size: 1
alias: author
resolve: values
permalink: blog/authors/{{ author.full_name | slugify }}/
--- |
Beta Was this translation helpful? Give feedback.
Turns out that the 11ty pagination returns keys by default. I'm not sure why I can't access the
authors
global data item, but this seems to at least give me the full author object, which is what I need...In
/src/author.njk
, changed the frontmatter like so: