Skip to content

Access child elements in front matter without knowing their names #2425

Answered by pdehaan
werls asked this question in Q&A
Discussion options

You must be logged in to vote

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.

"When iterating a hash, item[0] contains the key, and item[1] contains the value"
— via https://liquidjs.com/tags/for.html

---
links:
  behance: https://be.net
  pinterest: https://pinterest.com
  instagram: https:…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@pdehaan
Comment options

@werls
Comment options

@btrem
Comment options

Answer selected by werls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants