demiton
demiton

Reputation: 715

is possible to loop through yaml variables of jekyll page without knowing them?

I want to iterate with liquid through the list of the yaml variables of Jekyll page without knowing their names and print their key / value.

I illustrate my question with an "object" (wrong) formalism :

As :

{% assign variables = page.data %}
{% for var in variables %}
 key-name : {{var.key}} , value : {{var.value}} , 
{% endfor%}

And thus for a post file with the simple yaml front matter :

---
layout: post
title: MyBlog
---

I want to get :

key-name : layout , value : post
key-name : title , value : MyBlog

To be precise, I don't want to loop inside one of the variable itself as this question as I don't know the structure of the Yaml front matter of my jekyll posts because they are each different.

Probably I'm wrong, but I don't find any clear liquid syntax which could do the job.

Thanks

Upvotes: 0

Views: 1217

Answers (1)

David Jacquel
David Jacquel

Reputation: 52799

When you loop on a Page (Page objects like index, about, ...) variables, you receive arrays like :

{% for var in page %}
  {{ var | inspect }} => ["layout", "default"]

If you want to print key value pairs, you just have to call array elements by their indexes :

{% for var in page %}
  key-name : {{ var[0] }} , value : {{ var[1] }}
{% endfor%}

Edit : Which is not the same with Collections, where "pages" are in fact Document objects (posts or any custom collection)

Here you have to call key/value like this :

{% for var in page %}
      key : {{ var }} , value : {{ page[var] }}
{% endfor%}

Upvotes: 4

Related Questions