-
Hi! I'm really impressed with all the creative possibilities using eleventy. I must say that I still consider myself a beginner programmer and eleventy was my first direct contact with template systems like liquid and nunjucks. I have a question: is that possible to access child elements in front matter data without knowing their names? Here's my example. I'm using the front matter in a md file to set a bunch of links that I want it to be listed on the page:
The names and links will be different on each page, so I cannot know their names in advance. My goal is to render an html block with a list of the names, each one wrapped in a
As the .url and .name keys don't exist, this is not a real possibility. And, if I render a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you don't mind tweaking your front matter slightly this seems to work w/ Nunjucks: ---
# Note that I converted it from arrays w/ 1 key/value to an object.
links:
behance: https://be.net
pinterest: https://pinterest.com
instagram: https://instagram.com
---
<ul>
{% for name, url in links %}
<li><a href="{{ url }}">{{ name }}</a></li>
{% endfor %}
</ul> LiquidJS doesn't support the same array looping syntax (so I had to use array notation), but this tweak worked.
---
links:
behance: https://be.net
pinterest: https://pinterest.com
instagram: https://instagram.com
---
<ul>
{% for link in links %}
<li><a href="{{ link[1] }}">{{ link[0] }}</a></li>
{% endfor %}
</ul> Where your original YAML: links:
- behance: https://be.net
- pinterest: https://pinterest.com
- instagram: https://instagram.com Converts to the following JSON: {
"links": [
{
"behance": "https://be.net"
},
{
"pinterest": "https://pinterest.com"
},
{
"instagram": "https://instagram.com"
}
]
} |
Beta Was this translation helpful? Give feedback.
If you don't mind tweaking your front matter slightly this seems to work w/ Nunjucks:
LiquidJS doesn't support the same array looping syntax (so I had to use array notation), but this tweak worked.